From 5343338018ef79ce6c9d6dd938134f318d1ae38f Mon Sep 17 00:00:00 2001 From: nange Date: Wed, 24 Apr 2024 19:44:39 +0800 Subject: [PATCH] feat: support Cloudflare Preferred-IP --- config.go | 5 +++++ easyss.go | 13 ++++++++++--- httptunnel/local_conn.go | 6 +++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 303efa75..9757c963 100644 --- a/config.go +++ b/config.go @@ -121,6 +121,7 @@ type ServerConfig struct { CAPath string `json:"ca_path,omitempty"` Default bool `json:"default,omitempty"` OutboundProto string `json:"outbound_proto,omitempty"` + SN string `json:"sn"` CMDBeforeStartup string `json:"cmd_before_startup,omitempty"` CMDInterval string `json:"cmd_interval,omitempty"` CMDIntervalTime int `json:"cmd_interval_time,omitempty"` @@ -152,6 +153,7 @@ type Config struct { IPV6Rule string `json:"ipv6_rule"` CAPath string `json:"ca_path"` OutboundProto string `json:"outbound_proto"` + SN string `json:"sn"` CMDBeforeStartup string `json:"cmd_before_startup"` CMDInterval string `json:"cmd_interval"` CMDIntervalTime int `json:"cmd_interval_time"` @@ -379,6 +381,9 @@ func (c *Config) OverrideFrom(sc *ServerConfig) { if sc.OutboundProto != "" { c.OutboundProto = sc.OutboundProto } + if sc.SN != "" { + c.SN = sc.SN + } if sc.CMDInterval != "" { c.CMDInterval = sc.CMDInterval } diff --git a/easyss.go b/easyss.go index 31ed1f84..47e64a9d 100644 --- a/easyss.go +++ b/easyss.go @@ -496,7 +496,7 @@ func (ss *Easyss) InitTcpPool() error { uConn := utls.UClient( conn, &utls.Config{ - ServerName: ss.Server(), + ServerName: ss.ServerName(), RootCAs: certPool, }, utls.HelloChrome_Auto) @@ -651,6 +651,13 @@ func (ss *Easyss) Server() string { return ss.currConfig.Server } +func (ss *Easyss) ServerName() string { + if ss.currConfig.SN != "" { + return ss.currConfig.SN + } + return ss.currConfig.Server +} + func (ss *Easyss) DirectDNSServer() string { return ss.directDNSServer } @@ -841,9 +848,9 @@ func (ss *Easyss) AvailableConn(needPingACK ...bool) (conn net.Conn, err error) for i := 0; i < tryCount; i++ { switch ss.OutboundProto() { case OutboundProtoHTTP: - conn, err = httptunnel.NewLocalConn(ss.HTTPOutboundClient(), "http://"+ss.ServerAddr()) + conn, err = httptunnel.NewLocalConn(ss.HTTPOutboundClient(), "http://"+ss.ServerAddr(), ss.ServerName()) case OutboundProtoHTTPS: - conn, err = httptunnel.NewLocalConn(ss.HTTPOutboundClient(), "https://"+ss.ServerAddr()) + conn, err = httptunnel.NewLocalConn(ss.HTTPOutboundClient(), "https://"+ss.ServerAddr(), ss.ServerName()) default: conn, err = pool.Get() } diff --git a/httptunnel/local_conn.go b/httptunnel/local_conn.go index e46ecb66..b53305af 100644 --- a/httptunnel/local_conn.go +++ b/httptunnel/local_conn.go @@ -27,6 +27,7 @@ const ( type LocalConn struct { uuid string serverAddr string + serverName string conn net.Conn conn2 net.Conn @@ -35,7 +36,7 @@ type LocalConn struct { left []byte } -func NewLocalConn(client *req.Client, serverAddr string) (net.Conn, error) { +func NewLocalConn(client *req.Client, serverAddr, serverName string) (net.Conn, error) { if client == nil { return nil, errors.New("http outbound client is nil") } @@ -48,6 +49,7 @@ func NewLocalConn(client *req.Client, serverAddr string) (net.Conn, error) { lc := &LocalConn{ uuid: id.String(), serverAddr: serverAddr, + serverName: serverName, conn: conn, conn2: conn2, client: client, @@ -130,6 +132,7 @@ func (l *LocalConn) pull() error { SetQueryParam("transaction_id", p.TransactionID). SetQueryParam("access_token", p.AccessToken). SetQueryParam(RequestIDQuery, l.uuid). + SetHeader("Host", l.serverName). SetHeader("Accept-Encoding", "gzip"). Get(l.serverAddr + "/pull") if err != nil { @@ -146,6 +149,7 @@ func (l *LocalConn) pull() error { func (l *LocalConn) push() error { r := l.client.R(). + SetHeader("Host", l.serverName). SetHeader("Content-Type", "application/json"). SetHeader("Transfer-Encoding", "chunked"). SetHeader("Accept-Encoding", "gzip").