Skip to content

Commit d57ba31

Browse files
committed
PR Comments
1 parent 16cfdd7 commit d57ba31

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

lib/libp2phttp.go

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"net"
1414
"net/http"
1515
"net/http/httputil"
16+
"net/url"
17+
"strconv"
1618
"time"
1719

1820
"github.com/libp2p/go-libp2p/core/host"
@@ -29,44 +31,64 @@ func Libp2pHTTPSocketProxy(ctx context.Context, p multiaddr.Multiaddr, unixSocke
2931

3032
httpHost := libp2phttp.Host{StreamHost: h}
3133

32-
ai := peer.AddrInfo{
33-
Addrs: []multiaddr.Multiaddr{p},
34+
ai, err := peer.AddrInfoFromP2pAddr(p)
35+
if err == peer.ErrInvalidAddr {
36+
ai = &peer.AddrInfo{Addrs: []multiaddr.Multiaddr{p}} // No peer id
37+
err = nil
3438
}
35-
idStr, err := p.ValueForProtocol(multiaddr.P_P2P)
36-
if err == nil {
37-
id, err := peer.Decode(idStr)
38-
if err != nil {
39-
return err
40-
}
41-
ai.ID = id
39+
if err != nil {
40+
return err
4241
}
4342

4443
hasTLS := false
4544
hasHTTP := false
45+
host := ""
46+
port := 0
4647
multiaddr.ForEach(p, func(c multiaddr.Component) bool {
47-
if c.Protocol().Code == multiaddr.P_HTTP {
48+
switch c.Protocol().Code {
49+
case multiaddr.P_TLS:
50+
hasTLS = true
51+
case multiaddr.P_HTTP:
4852
hasHTTP = true
49-
}
50-
51-
if c.Protocol().Code == multiaddr.P_HTTPS {
53+
case multiaddr.P_HTTPS:
5254
hasHTTP = true
5355
hasTLS = true
56+
case multiaddr.P_IP4, multiaddr.P_IP6, multiaddr.P_DNS4, multiaddr.P_DNS6, multiaddr.P_DNS:
57+
host = c.Value()
58+
case multiaddr.P_TCP, multiaddr.P_UDP:
59+
port, err = strconv.Atoi(c.Value())
5460
return false
5561
}
56-
57-
if c.Protocol().Code == multiaddr.P_TLS {
58-
hasTLS = true
59-
}
6062
return true
6163
})
64+
if err != nil {
65+
return err
66+
}
67+
if port == 0 && hasHTTP {
68+
port = 80
69+
if hasTLS {
70+
port = 443
71+
}
72+
}
6273

63-
rt, err := httpHost.NewConstrainedRoundTripper(ai)
74+
rt, err := httpHost.NewConstrainedRoundTripper(*ai)
6475
if err != nil {
6576
return err
6677
}
67-
rp := &httputil.ReverseProxy{
68-
Transport: rt,
69-
Director: func(r *http.Request) {},
78+
79+
var rp http.Handler
80+
if hasTLS && hasHTTP {
81+
u, err := url.Parse("https://" + host + ":" + strconv.Itoa(port) + "/")
82+
if err != nil {
83+
return err
84+
}
85+
revProxy := httputil.NewSingleHostReverseProxy(u)
86+
rp = revProxy
87+
} else {
88+
rp = &httputil.ReverseProxy{
89+
Transport: rt,
90+
Director: func(r *http.Request) {},
91+
}
7092
}
7193

7294
// Serves an HTTP server on the given path using unix sockets
@@ -101,8 +123,8 @@ func Libp2pHTTPSocketProxy(ctx context.Context, p multiaddr.Multiaddr, unixSocke
101123
return server.Serve(l)
102124
}
103125

104-
// Libp2pHTTPServer serves an libp2p enabled HTTP server
105-
func Libp2pHTTPServer() (host.Host, *libp2phttp.Host, error) {
126+
// libp2pHTTPServer serves an libp2p enabled HTTP server
127+
func libp2pHTTPServer() (host.Host, *libp2phttp.Host, error) {
106128
h, err := libp2pHost()
107129
if err != nil {
108130
return nil, nil, err

lib/libp2phttp_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,25 @@ import (
1313

1414
func TestHTTPProxyAndServer(t *testing.T) {
1515
// Start libp2p HTTP server
16-
h, hh, err := Libp2pHTTPServer()
16+
h, hh, err := libp2pHTTPServer()
1717
if err != nil {
1818
t.Fatal(err)
1919
}
20+
hh.SetHTTPHandlerAtPath("/hello", "/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21+
w.WriteHeader(http.StatusOK)
22+
}))
2023

2124
go hh.Serve()
2225
defer hh.Close()
2326

2427
serverAddr := h.Addrs()[0].Encapsulate(multiaddr.StringCast("/p2p/" + h.ID().String()))
28+
port, err := serverAddr.ValueForProtocol(multiaddr.P_TCP)
29+
if err != nil || port == "" {
30+
port, err = serverAddr.ValueForProtocol(multiaddr.P_UDP)
31+
if err != nil || port == "" {
32+
t.Fatal("could not get port from server address")
33+
}
34+
}
2535

2636
ctx := context.Background()
2737
ctx, cancel := context.WithCancel(ctx)
@@ -59,8 +69,7 @@ func TestHTTPProxyAndServer(t *testing.T) {
5969
},
6070
}
6171

62-
// TODO update this when https://github.com/libp2p/go-libp2p/pull/2757 lands
63-
resp, err := client.Get("http://example.com" + "/.well-known/libp2p")
72+
resp, err := client.Get("http://127.0.0.1:" + port + "/")
6473
if err != nil {
6574
t.Fatal(err)
6675
}
@@ -128,8 +137,7 @@ func TestHTTPProxyAndServerOverHTTPTransport(t *testing.T) {
128137
},
129138
}
130139

131-
// TODO update this when https://github.com/libp2p/go-libp2p/pull/2757 lands
132-
resp, err := client.Get("http://example.com/")
140+
resp, err := client.Get("http://127.0.0.1:" + port + "/")
133141
if err != nil {
134142
t.Fatal(err)
135143
}

0 commit comments

Comments
 (0)