Skip to content

Commit

Permalink
feat: support Cloudflare Preferred-IP
Browse files Browse the repository at this point in the history
  • Loading branch information
nange committed Apr 24, 2024
1 parent 42f51af commit 5343338
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 10 additions & 3 deletions easyss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}
Expand Down
6 changes: 5 additions & 1 deletion httptunnel/local_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
type LocalConn struct {
uuid string
serverAddr string
serverName string
conn net.Conn
conn2 net.Conn

Expand All @@ -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")
}
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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").
Expand Down

0 comments on commit 5343338

Please sign in to comment.