2019-12-08 04:17:24 +00:00
|
|
|
package outbound
|
2018-12-03 15:27:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-10-12 15:55:39 +00:00
|
|
|
"context"
|
2018-12-03 15:27:00 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-12-05 10:22:07 +00:00
|
|
|
"net/url"
|
2018-12-03 15:27:00 +00:00
|
|
|
"strconv"
|
|
|
|
|
2020-02-15 13:42:46 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2018-12-03 15:27:00 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Http struct {
|
2018-12-22 15:56:42 +00:00
|
|
|
*Base
|
2019-12-05 10:22:07 +00:00
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
tlsConfig *tls.Config
|
2018-12-03 15:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
2020-10-02 03:34:40 +00:00
|
|
|
SNI string `proxy:"sni,omitempty"`
|
2018-12-03 15:27:00 +00:00
|
|
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// StreamConn implements C.ProxyAdapter
|
2020-03-21 15:46:49 +00:00
|
|
|
func (h *Http) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|
|
|
if h.tlsConfig != nil {
|
2018-12-03 15:27:00 +00:00
|
|
|
cc := tls.Client(c, h.tlsConfig)
|
2020-03-21 15:46:49 +00:00
|
|
|
err := cc.Handshake()
|
2018-12-03 15:27:00 +00:00
|
|
|
c = cc
|
2020-03-21 15:46:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", h.addr, err)
|
|
|
|
}
|
2018-12-03 15:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 15:46:49 +00:00
|
|
|
if err := h.shakeHand(metadata, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-03-22 15:26:20 +00:00
|
|
|
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
2020-03-21 15:46:49 +00:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", h.addr)
|
2018-12-03 15:27:00 +00:00
|
|
|
if err != nil {
|
2019-12-05 09:51:21 +00:00
|
|
|
return nil, fmt.Errorf("%s connect error: %w", h.addr, err)
|
2018-12-03 15:27:00 +00:00
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
2020-03-21 15:46:49 +00:00
|
|
|
|
2021-03-22 15:26:20 +00:00
|
|
|
defer safeConnClose(c, err)
|
|
|
|
|
2020-03-21 15:46:49 +00:00
|
|
|
c, err = h.StreamConn(c, metadata)
|
|
|
|
if err != nil {
|
2018-12-03 15:27:00 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-21 15:46:49 +00:00
|
|
|
return NewConn(c, h), nil
|
2018-12-03 15:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Http) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
|
2019-10-11 12:11:18 +00:00
|
|
|
addr := metadata.RemoteAddress()
|
2019-12-05 10:22:07 +00:00
|
|
|
req := &http.Request{
|
|
|
|
Method: http.MethodConnect,
|
|
|
|
URL: &url.URL{
|
|
|
|
Host: addr,
|
|
|
|
},
|
|
|
|
Host: addr,
|
|
|
|
Header: http.Header{
|
|
|
|
"Proxy-Connection": []string{"Keep-Alive"},
|
|
|
|
},
|
|
|
|
}
|
2018-12-03 15:27:00 +00:00
|
|
|
|
|
|
|
if h.user != "" && h.pass != "" {
|
|
|
|
auth := h.user + ":" + h.pass
|
2019-12-05 10:22:07 +00:00
|
|
|
req.Header.Add("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
|
2018-12-03 15:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 10:22:07 +00:00
|
|
|
if err := req.Write(rw); err != nil {
|
2018-12-03 15:27:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-05 10:22:07 +00:00
|
|
|
resp, err := http.ReadResponse(bufio.NewReader(rw), req)
|
2018-12-03 15:27:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-05 01:33:40 +00:00
|
|
|
if resp.StatusCode == http.StatusOK {
|
2018-12-03 15:27:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-05 01:33:40 +00:00
|
|
|
if resp.StatusCode == http.StatusProxyAuthRequired {
|
2018-12-03 15:27:00 +00:00
|
|
|
return errors.New("HTTP need auth")
|
|
|
|
}
|
|
|
|
|
2019-10-05 01:33:40 +00:00
|
|
|
if resp.StatusCode == http.StatusMethodNotAllowed {
|
2018-12-03 15:27:00 +00:00
|
|
|
return errors.New("CONNECT method not allowed by proxy")
|
|
|
|
}
|
2019-10-05 01:33:40 +00:00
|
|
|
|
|
|
|
if resp.StatusCode >= http.StatusInternalServerError {
|
|
|
|
return errors.New(resp.Status)
|
|
|
|
}
|
2019-12-05 10:22:07 +00:00
|
|
|
|
2018-12-03 15:27:00 +00: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 {
|
2020-10-02 03:34:40 +00:00
|
|
|
sni := option.Server
|
|
|
|
if option.SNI != "" {
|
|
|
|
sni = option.SNI
|
|
|
|
}
|
2018-12-03 15:27:00 +00:00
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: option.SkipCertVerify,
|
2020-10-02 03:34:40 +00:00
|
|
|
ServerName: sni,
|
2018-12-03 15:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Http{
|
2018-12-22 15:56:42 +00:00
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
2020-03-21 15:46:49 +00:00
|
|
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
2018-12-22 15:56:42 +00:00
|
|
|
tp: C.Http,
|
|
|
|
},
|
2019-12-05 10:22:07 +00:00
|
|
|
user: option.UserName,
|
|
|
|
pass: option.Password,
|
|
|
|
tlsConfig: tlsConfig,
|
2018-12-03 15:27:00 +00:00
|
|
|
}
|
|
|
|
}
|