Improve: config convergent and add log-level
This commit is contained in:
@ -1,77 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type HttpAdapter struct {
|
||||
addr *C.Addr
|
||||
r *http.Request
|
||||
w http.ResponseWriter
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func (h *HttpAdapter) Close() {
|
||||
h.done <- struct{}{}
|
||||
}
|
||||
|
||||
func (h *HttpAdapter) Addr() *C.Addr {
|
||||
return h.addr
|
||||
}
|
||||
|
||||
func (h *HttpAdapter) Connect(proxy C.ProxyAdapter) {
|
||||
req := http.Transport{
|
||||
Dial: func(string, string) (net.Conn, error) {
|
||||
return proxy.Conn(), nil
|
||||
},
|
||||
// from http.DefaultTransport
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
resp, err := req.RoundTrip(h.r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
header := h.w.Header()
|
||||
for k, vv := range resp.Header {
|
||||
for _, v := range vv {
|
||||
header.Add(k, v)
|
||||
}
|
||||
}
|
||||
h.w.WriteHeader(resp.StatusCode)
|
||||
var writer io.Writer = h.w
|
||||
if len(resp.TransferEncoding) > 0 && resp.TransferEncoding[0] == "chunked" {
|
||||
writer = ChunkWriter{Writer: h.w}
|
||||
}
|
||||
io.Copy(writer, resp.Body)
|
||||
}
|
||||
|
||||
type ChunkWriter struct {
|
||||
io.Writer
|
||||
}
|
||||
|
||||
func (cw ChunkWriter) Write(b []byte) (int, error) {
|
||||
n, err := cw.Writer.Write(b)
|
||||
if err == nil {
|
||||
cw.Writer.(http.Flusher).Flush()
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func NewHttp(host string, w http.ResponseWriter, r *http.Request) (*HttpAdapter, chan struct{}) {
|
||||
done := make(chan struct{})
|
||||
return &HttpAdapter{
|
||||
addr: parseHttpAddr(host),
|
||||
r: r,
|
||||
w: w,
|
||||
done: done,
|
||||
}, done
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type HttpsAdapter struct {
|
||||
addr *C.Addr
|
||||
conn net.Conn
|
||||
rw *bufio.ReadWriter
|
||||
}
|
||||
|
||||
func (h *HttpsAdapter) Close() {
|
||||
h.conn.Close()
|
||||
}
|
||||
|
||||
func (h *HttpsAdapter) Addr() *C.Addr {
|
||||
return h.addr
|
||||
}
|
||||
|
||||
func (h *HttpsAdapter) Connect(proxy C.ProxyAdapter) {
|
||||
go io.Copy(h.conn, proxy.ReadWriter())
|
||||
io.Copy(proxy.ReadWriter(), h.conn)
|
||||
}
|
||||
|
||||
func NewHttps(host string, conn net.Conn) *HttpsAdapter {
|
||||
return &HttpsAdapter{
|
||||
addr: parseHttpAddr(host),
|
||||
conn: conn,
|
||||
}
|
||||
}
|
@ -6,15 +6,15 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Dreamacro/clash/adapters/local"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/tunnel"
|
||||
|
||||
"github.com/riobard/go-shadowsocks2/socks"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
tun = tunnel.GetInstance()
|
||||
tun = tunnel.Instance()
|
||||
)
|
||||
|
||||
func NewHttpProxy(addr string) (*C.ProxySignal, error) {
|
||||
@ -61,7 +61,7 @@ func handleHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr += ":80"
|
||||
}
|
||||
req, done := NewHttp(addr, w, r)
|
||||
req, done := adapters.NewHttp(addr, w, r)
|
||||
tun.Add(req)
|
||||
<-done
|
||||
}
|
||||
@ -77,33 +77,5 @@ func handleTunneling(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// w.WriteHeader(http.StatusOK) doesn't works in Safari
|
||||
conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
|
||||
tun.Add(NewHttps(r.Host, conn))
|
||||
}
|
||||
|
||||
func parseHttpAddr(target string) *C.Addr {
|
||||
host, port, _ := net.SplitHostPort(target)
|
||||
ipAddr, err := net.ResolveIPAddr("ip", host)
|
||||
var resolveIP *net.IP
|
||||
if err == nil {
|
||||
resolveIP = &ipAddr.IP
|
||||
}
|
||||
|
||||
var addType int
|
||||
ip := net.ParseIP(host)
|
||||
switch {
|
||||
case ip == nil:
|
||||
addType = socks.AtypDomainName
|
||||
case ip.To4() == nil:
|
||||
addType = socks.AtypIPv6
|
||||
default:
|
||||
addType = socks.AtypIPv4
|
||||
}
|
||||
|
||||
return &C.Addr{
|
||||
NetWork: C.TCP,
|
||||
AddrType: addType,
|
||||
Host: host,
|
||||
IP: resolveIP,
|
||||
Port: port,
|
||||
}
|
||||
tun.Add(adapters.NewHttps(r.Host, conn))
|
||||
}
|
||||
|
Reference in New Issue
Block a user