2018-12-03 23:27:00 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2019-10-12 23:55:39 +08:00
|
|
|
"context"
|
2018-12-03 23:27:00 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Http struct {
|
2018-12-22 23:56:42 +08:00
|
|
|
*Base
|
2018-12-03 23:27:00 +08:00
|
|
|
addr string
|
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
tls bool
|
|
|
|
skipCertVerify bool
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type HttpOption struct {
|
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
UserName string `proxy:"username,omitempty"`
|
|
|
|
Password string `proxy:"password,omitempty"`
|
|
|
|
TLS bool `proxy:"tls,omitempty"`
|
|
|
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-10-12 23:55:39 +08:00
|
|
|
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
|
|
|
c, err := dialContext(ctx, "tcp", h.addr)
|
2018-12-03 23:27:00 +08:00
|
|
|
if err == nil && h.tls {
|
|
|
|
cc := tls.Client(c, h.tlsConfig)
|
|
|
|
err = cc.Handshake()
|
|
|
|
c = cc
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error", h.addr)
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
if err := h.shakeHand(metadata, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
return newConn(c, h), nil
|
2018-12-03 23:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Http) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
var err error
|
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
addr := metadata.RemoteAddress()
|
2019-01-07 10:47:25 +08:00
|
|
|
buf.WriteString("CONNECT " + addr + " HTTP/1.1\r\n")
|
2019-02-14 11:37:47 +08:00
|
|
|
buf.WriteString("Host: " + metadata.String() + "\r\n")
|
2018-12-03 23:27:00 +08:00
|
|
|
buf.WriteString("Proxy-Connection: Keep-Alive\r\n")
|
|
|
|
|
|
|
|
if h.user != "" && h.pass != "" {
|
|
|
|
auth := h.user + ":" + h.pass
|
|
|
|
buf.WriteString("Proxy-Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + "\r\n")
|
|
|
|
}
|
|
|
|
// header ended
|
|
|
|
buf.WriteString("\r\n")
|
|
|
|
|
|
|
|
_, err = rw.Write(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var req http.Request
|
|
|
|
resp, err := http.ReadResponse(bufio.NewReader(rw), &req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-05 09:33:40 +08:00
|
|
|
if resp.StatusCode == http.StatusOK {
|
2018-12-03 23:27:00 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-05 09:33:40 +08:00
|
|
|
if resp.StatusCode == http.StatusProxyAuthRequired {
|
2018-12-03 23:27:00 +08:00
|
|
|
return errors.New("HTTP need auth")
|
|
|
|
}
|
|
|
|
|
2019-10-05 09:33:40 +08:00
|
|
|
if resp.StatusCode == http.StatusMethodNotAllowed {
|
2018-12-03 23:27:00 +08:00
|
|
|
return errors.New("CONNECT method not allowed by proxy")
|
|
|
|
}
|
2019-10-05 09:33:40 +08:00
|
|
|
|
|
|
|
if resp.StatusCode >= http.StatusInternalServerError {
|
|
|
|
return errors.New(resp.Status)
|
|
|
|
}
|
2018-12-03 23:27:00 +08:00
|
|
|
return fmt.Errorf("can not connect remote err code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHttp(option HttpOption) *Http {
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if option.TLS {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: option.SkipCertVerify,
|
|
|
|
ClientSessionCache: getClientSessionCache(),
|
|
|
|
ServerName: option.Server,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Http{
|
2018-12-22 23:56:42 +08:00
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
|
|
|
tp: C.Http,
|
|
|
|
},
|
2018-12-03 23:27:00 +08:00
|
|
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
|
|
|
user: option.UserName,
|
|
|
|
pass: option.Password,
|
|
|
|
tls: option.TLS,
|
|
|
|
skipCertVerify: option.SkipCertVerify,
|
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
}
|