Skip to content

Commit

Permalink
new: add xray fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Jul 27, 2024
1 parent e9a7563 commit 2c4d7d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 6 additions & 1 deletion option/xray.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ type XrayOutboundOptions struct {
DialerOptions
Network NetworkList `json:"network,omitempty"`
UDPOverTCP *UDPOverTCPOptions `json:"udp_over_tcp,omitempty"`
XrayOutboundJson interface{} `json:"xray_outbound_raw"`
XrayOutboundJson map[string]any `json:"xray_outbound_raw"`
Fragment *struct {
Packets string `json:"packets"`
Length string `json:"length"`
Interval string `json:"interval"`
} `json:"fragment"`
}
33 changes: 31 additions & 2 deletions outbound/xray.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"

dns "github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
Expand Down Expand Up @@ -91,7 +92,7 @@ func getRandomFreePort() uint16 {
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err == nil {
l.Close()
l, err := net.Listen("udp", fmt.Sprintf("127.0.0.1:%d", port))
l, err := net.ListenPacket("udp", fmt.Sprintf("127.0.0.1:%d", port))
if err == nil {
l.Close()
return uint16(port)
Expand All @@ -106,6 +107,33 @@ func NewXray(ctx context.Context, router adapter.Router, logger log.ContextLogge
}
userpass := newuuid.String()
port := getRandomFreePort()
outbounds := []map[string]any{options.XrayOutboundJson}
outbounds[0]["sockopt"] = map[string]any{}
if options.Fragment != nil {
outbounds[0]["sockopt"] = map[string]any{
"dialerProxy": "fragment",
"tcpKeepAliveIdle": 100,
"tcpNoDelay": true,
}
outbounds = append(outbounds, map[string]any{
"tag": "fragment",
"protocol": "freedom",
"settings": map[string]any{
"domainStrategy": "AsIs",
"fragment": map[string]any{
"packets": options.Fragment.Packets,
"length": options.Fragment.Length,
"interval": options.Fragment.Interval,
},
},
"streamSettings": map[string]any{
"sockopt": map[string]any{
"tcpKeepAliveIdle": 100,
"tcpNoDelay": true,
},
},
})
}
xray := map[string]any{
"inbounds": []any{
map[string]any{
Expand All @@ -124,8 +152,9 @@ func NewXray(ctx context.Context, router adapter.Router, logger log.ContextLogge
},
},
},
"outbounds": []any{options.XrayOutboundJson},
"outbounds": outbounds,
}

jsonData, err := json.MarshalIndent(xray, "", " ")
if err != nil {
fmt.Printf("Error marshaling to JSON: %v", err)
Expand Down

0 comments on commit 2c4d7d6

Please sign in to comment.