Use go-libp2p-http
License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Oct 5, 2018 at 00:01 UTC
f03efbd200c34ba1e98b699d3ebb46df92e72701
2 files changed
+18
-78
core/corehttp/proxy.go
+12
-78
@@ -1,19 +1,17 @@
1
package corehttp
2
3
import (
4
- "bufio"
4
"fmt"
6
- "io"
5
"net"
6
"net/http"
7
"net/http/httputil"
8
+ "net/url"
9
"strings"
10
11
core "github.com/ipfs/go-ipfs/core"
12
13
protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
15
- peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
16
- inet "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
14
+ p2phttp "gx/ipfs/QmcLYfmHLsaVRKGMZQovwEYhHAjWtRjg1Lij3pnzw5UkRD/go-libp2p-http"
15
)
16
17
// ProxyOption is an endpoint for proxying a HTTP request to another ipfs peer
@@ -27,23 +25,24 @@ func ProxyOption() ServeOption {
25
return
26
}
27
30
- // open connect to peer
31
- stream, err := ipfsNode.P2P.PeerHost.NewStream(request.Context(), parsedRequest.target, protocol.ID("/x/"+parsedRequest.name))
28
+ target, err := url.Parse(fmt.Sprintf("libp2p://%s/%s", parsedRequest.target, parsedRequest.httpPath))
29
if err != nil {
33
- msg := fmt.Sprintf("Failed to open stream '%v' to target peer '%v'", parsedRequest.name, parsedRequest.target)
34
- handleError(w, msg, err, 500)
30
+ handleError(w, "Failed to parse url", err, 400)
31
return
32
}
37
- //send proxy request and response to client
38
- newReverseHTTPProxy(parsedRequest, stream).ServeHTTP(w, request)
33
+
34
+ rt := p2phttp.NewTransport(ipfsNode.P2P.PeerHost, p2phttp.ProtocolOption(parsedRequest.name))
35
+ proxy := httputil.NewSingleHostReverseProxy(target)
36
+ proxy.Transport = rt
37
+ proxy.ServeHTTP(w, request)
38
})
39
return mux, nil
40
}
41
}
42
43
type proxyRequest struct {
45
- target peer.ID
46
- name string
44
+ target string
45
+ name protocol.ID
46
httpPath string // path to send to the proxy-host
47
}
48
@@ -57,13 +56,7 @@ func parseRequest(request *http.Request) (*proxyRequest, error) {
56
return nil, fmt.Errorf("Invalid request path '%s'", path)
57
}
58
60
- peerID, err := peer.IDB58Decode(split[3])
61
-
62
- if err != nil {
63
- return nil, err
64
- }
65
-
66
- return &proxyRequest{peerID, split[4], "/" + split[5]}, nil
59
+ return &proxyRequest{split[3], protocol.ID(split[4]), "/" + split[5]}, nil
60
}
61
62
func handleError(w http.ResponseWriter, msg string, err error, code int) {
@@ -71,62 +64,3 @@ func handleError(w http.ResponseWriter, msg string, err error, code int) {
64
fmt.Fprintf(w, "%s: %s\n", msg, err)
65
log.Warningf("server error: %s: %s", err)
66
}
74
-
75
-func newReverseHTTPProxy(req *proxyRequest, streamToPeer inet.Stream) *httputil.ReverseProxy {
76
- director := func(r *http.Request) {
77
- r.URL.Path = req.httpPath //the scheme etc. doesn't matter
78
- }
79
-
80
- return &httputil.ReverseProxy{
81
- Director: director,
82
- Transport: &roundTripper{streamToPeer}}
83
-}
84
-
85
-type roundTripper struct {
86
- stream inet.Stream
87
-}
88
-
89
-// we wrap the response body and close the stream
90
-// only when it's closed.
91
-type respBody struct {
92
- io.ReadCloser
93
- stream inet.Stream
94
-}
95
-
96
-// Closes the response's body and the connection.
97
-func (rb *respBody) Close() error {
98
- if err := rb.stream.Close(); err != nil {
99
- rb.stream.Reset()
100
- } else {
101
- go inet.AwaitEOF(rb.stream)
102
- }
103
- return rb.ReadCloser.Close()
104
-}
105
-
106
-func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
107
-
108
- sendRequest := func() {
109
- err := req.Write(rt.stream)
110
- if err != nil {
111
- rt.stream.Close()
112
- }
113
- if req.Body != nil {
114
- req.Body.Close()
115
- }
116
- }
117
- //send request while reading response
118
- go sendRequest()
119
- s := bufio.NewReader(rt.stream)
120
-
121
- resp, err := http.ReadResponse(s, req)
122
- if err != nil {
123
- return resp, err
124
- }
125
-
126
- resp.Body = &respBody{
127
- ReadCloser: resp.Body,
128
- stream: rt.stream,
129
- }
130
-
131
- return resp, nil
132
-}
package.json
+6
@@ -592,6 +592,12 @@
592
"hash": "QmTqLBwme9BusYWdACqL62NFb8WV2Q72gXLsQVfC7vmCr4",
593
"name": "iptb-plugins",
594
"version": "1.0.5"
595
+ },
596
+ {
597
+ "author": "hsanjuan",
598
+ "hash": "QmcLYfmHLsaVRKGMZQovwEYhHAjWtRjg1Lij3pnzw5UkRD",
599
+ "name": "go-libp2p-http",
600
+ "version": "1.1.8"
601
}
602
],
603
"gxVersion": "0.10.0",