mirror of https://github.com/ehang-io/nps
fix client use http proxy
parent
0865c98a7f
commit
f522bdb6ee
|
@ -1,6 +1,7 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -11,7 +12,6 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -253,31 +253,28 @@ func NewConn(tp string, vkey string, server string, connType string, proxyUrl st
|
||||||
|
|
||||||
//http proxy connection
|
//http proxy connection
|
||||||
func NewHttpProxyConn(url *url.URL, remoteAddr string) (net.Conn, error) {
|
func NewHttpProxyConn(url *url.URL, remoteAddr string) (net.Conn, error) {
|
||||||
req := &http.Request{
|
req, err := http.NewRequest("CONNECT", "http://"+remoteAddr, nil)
|
||||||
Method: "CONNECT",
|
|
||||||
URL: url,
|
|
||||||
Host: remoteAddr,
|
|
||||||
Header: http.Header{},
|
|
||||||
ProtoMajor: 1,
|
|
||||||
ProtoMinor: 1,
|
|
||||||
}
|
|
||||||
password, _ := url.User.Password()
|
|
||||||
req.Header.Set("Authorization", "Basic "+basicAuth(strings.Trim(url.User.Username(), " "), password))
|
|
||||||
b, err := httputil.DumpRequest(req, false)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
password, _ := url.User.Password()
|
||||||
|
req.Header.Set("Authorization", "Basic "+basicAuth(strings.Trim(url.User.Username(), " "), password))
|
||||||
|
// we make a http proxy request
|
||||||
proxyConn, err := net.Dial("tcp", url.Host)
|
proxyConn, err := net.Dial("tcp", url.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if _, err := proxyConn.Write(b); err != nil {
|
if err := req.Write(proxyConn); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
buf := make([]byte, 1024)
|
res, err := http.ReadResponse(bufio.NewReader(proxyConn), req)
|
||||||
if _, err := proxyConn.Read(buf); err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
_ = res.Body.Close()
|
||||||
|
if res.StatusCode != 200 {
|
||||||
|
return nil, errors.New("Proxy error " + res.Status)
|
||||||
|
}
|
||||||
return proxyConn, nil
|
return proxyConn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue