2022-10-09 23:31:15 -04:00
|
|
|
package controller
|
2022-06-01 13:35:41 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-16 01:04:33 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/api/panel"
|
2022-07-28 11:00:05 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/conf"
|
2022-10-09 23:31:15 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/node/controller/legoCmd"
|
2022-10-11 04:05:19 -04:00
|
|
|
"github.com/goccy/go-json"
|
2022-06-01 13:35:41 -04:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/uuid"
|
|
|
|
"github.com/xtls/xray-core/core"
|
2022-07-28 11:00:05 -04:00
|
|
|
coreConf "github.com/xtls/xray-core/infra/conf"
|
2022-06-01 13:35:41 -04:00
|
|
|
)
|
|
|
|
|
2022-10-09 20:59:00 -04:00
|
|
|
// buildInbound build Inbound config for different protocol
|
|
|
|
func buildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag string) (*core.InboundHandlerConfig, error) {
|
2022-06-01 13:35:41 -04:00
|
|
|
var proxySetting interface{}
|
|
|
|
if nodeInfo.NodeType == "V2ray" {
|
2022-06-05 01:19:39 -04:00
|
|
|
defer func() {
|
2022-10-09 20:59:00 -04:00
|
|
|
//Clear v2ray config
|
2022-06-05 01:19:39 -04:00
|
|
|
nodeInfo.V2ray = nil
|
|
|
|
}()
|
2022-06-01 13:35:41 -04:00
|
|
|
if nodeInfo.EnableVless {
|
2022-10-09 20:59:00 -04:00
|
|
|
//Set vless
|
2022-06-01 13:35:41 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].Protocol = "vless"
|
|
|
|
if config.EnableFallback {
|
2022-10-09 20:59:00 -04:00
|
|
|
// Set fallback
|
2022-06-01 13:35:41 -04:00
|
|
|
fallbackConfigs, err := buildVlessFallbacks(config.FallBackConfigs)
|
|
|
|
if err == nil {
|
2022-07-28 11:00:05 -04:00
|
|
|
proxySetting = &coreConf.VLessInboundConfig{
|
2022-06-01 13:35:41 -04:00
|
|
|
Decryption: "none",
|
|
|
|
Fallbacks: fallbackConfigs,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-28 11:00:05 -04:00
|
|
|
proxySetting = &coreConf.VLessInboundConfig{
|
2022-06-01 13:35:41 -04:00
|
|
|
Decryption: "none",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-09 20:59:00 -04:00
|
|
|
// Set vmess
|
2022-06-01 13:35:41 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].Protocol = "vmess"
|
2022-07-28 11:00:05 -04:00
|
|
|
proxySetting = &coreConf.VMessInboundConfig{}
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|
|
|
|
} else if nodeInfo.NodeType == "Trojan" {
|
2022-06-05 01:19:39 -04:00
|
|
|
defer func() {
|
2022-10-09 20:59:00 -04:00
|
|
|
//clear trojan and v2ray config
|
2022-06-05 01:19:39 -04:00
|
|
|
nodeInfo.V2ray = nil
|
|
|
|
nodeInfo.Trojan = nil
|
|
|
|
}()
|
2022-08-16 01:04:33 -04:00
|
|
|
nodeInfo.V2ray = &panel.V2rayConfig{}
|
2022-07-28 11:00:05 -04:00
|
|
|
nodeInfo.V2ray.Inbounds = make([]coreConf.InboundDetourConfig, 1)
|
2022-06-01 13:35:41 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].Protocol = "trojan"
|
|
|
|
if config.EnableFallback {
|
2022-10-09 20:59:00 -04:00
|
|
|
// Set fallback
|
2022-06-01 13:35:41 -04:00
|
|
|
fallbackConfigs, err := buildTrojanFallbacks(config.FallBackConfigs)
|
|
|
|
if err == nil {
|
2022-07-28 11:00:05 -04:00
|
|
|
proxySetting = &coreConf.TrojanServerConfig{
|
2022-06-01 13:35:41 -04:00
|
|
|
Fallbacks: fallbackConfigs,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-28 11:00:05 -04:00
|
|
|
proxySetting = &coreConf.TrojanServerConfig{}
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].PortList = &coreConf.PortList{
|
|
|
|
Range: []coreConf.PortRange{{From: uint32(nodeInfo.Trojan.LocalPort), To: uint32(nodeInfo.Trojan.LocalPort)}},
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
t := coreConf.TransportProtocol(nodeInfo.Trojan.TransportProtocol)
|
|
|
|
nodeInfo.V2ray.Inbounds[0].StreamSetting = &coreConf.StreamConfig{Network: &t}
|
2022-06-01 13:35:41 -04:00
|
|
|
} else if nodeInfo.NodeType == "Shadowsocks" {
|
2022-06-05 01:19:39 -04:00
|
|
|
defer func() {
|
2022-10-09 20:59:00 -04:00
|
|
|
//Clear v2ray config
|
2022-06-05 01:19:39 -04:00
|
|
|
nodeInfo.V2ray = nil
|
|
|
|
}()
|
2022-08-16 01:04:33 -04:00
|
|
|
nodeInfo.V2ray = &panel.V2rayConfig{}
|
2022-07-28 11:00:05 -04:00
|
|
|
nodeInfo.V2ray.Inbounds = []coreConf.InboundDetourConfig{{Protocol: "shadowsocks"}}
|
|
|
|
proxySetting = &coreConf.ShadowsocksServerConfig{}
|
2022-06-01 13:35:41 -04:00
|
|
|
randomPasswd := uuid.New()
|
2022-07-28 11:00:05 -04:00
|
|
|
defaultSSuser := &coreConf.ShadowsocksUserConfig{
|
2022-06-01 13:35:41 -04:00
|
|
|
Cipher: "aes-128-gcm",
|
|
|
|
Password: randomPasswd.String(),
|
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
proxySetting, _ := proxySetting.(*coreConf.ShadowsocksServerConfig)
|
2022-06-01 13:35:41 -04:00
|
|
|
proxySetting.Users = append(proxySetting.Users, defaultSSuser)
|
2022-07-28 11:00:05 -04:00
|
|
|
proxySetting.NetworkList = &coreConf.NetworkList{"tcp", "udp"}
|
2022-06-01 13:35:41 -04:00
|
|
|
proxySetting.IVCheck = true
|
|
|
|
if config.DisableIVCheck {
|
|
|
|
proxySetting.IVCheck = false
|
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].PortList = &coreConf.PortList{
|
|
|
|
Range: []coreConf.PortRange{{From: uint32(nodeInfo.SS.Port), To: uint32(nodeInfo.SS.Port)}},
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
t := coreConf.TransportProtocol(nodeInfo.SS.TransportProtocol)
|
|
|
|
nodeInfo.V2ray.Inbounds[0].StreamSetting = &coreConf.StreamConfig{Network: &t}
|
2022-06-02 13:22:56 -04:00
|
|
|
} else {
|
2022-06-06 05:22:45 -04:00
|
|
|
return nil, fmt.Errorf("unsupported node type: %s, Only support: V2ray, Trojan, Shadowsocks", nodeInfo.NodeType)
|
2022-10-09 20:59:00 -04:00
|
|
|
}
|
2022-06-01 13:35:41 -04:00
|
|
|
// Build Listen IP address
|
|
|
|
ipAddress := net.ParseAddress(config.ListenIP)
|
2022-07-28 11:00:05 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].ListenOn = &coreConf.Address{Address: ipAddress}
|
2022-06-01 13:35:41 -04:00
|
|
|
// SniffingConfig
|
2022-07-28 11:00:05 -04:00
|
|
|
sniffingConfig := &coreConf.SniffingConfig{
|
2022-06-01 13:35:41 -04:00
|
|
|
Enabled: true,
|
2022-07-28 11:00:05 -04:00
|
|
|
DestOverride: &coreConf.StringList{"http", "tls"},
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|
|
|
|
if config.DisableSniffing {
|
|
|
|
sniffingConfig.Enabled = false
|
|
|
|
}
|
|
|
|
nodeInfo.V2ray.Inbounds[0].SniffingConfig = sniffingConfig
|
|
|
|
|
|
|
|
var setting json.RawMessage
|
|
|
|
|
|
|
|
// Build Protocol and Protocol setting
|
|
|
|
setting, err := json.Marshal(proxySetting)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("marshal proxy %s config fialed: %s", nodeInfo.NodeType, err)
|
|
|
|
}
|
|
|
|
if *nodeInfo.V2ray.Inbounds[0].StreamSetting.Network == "tcp" {
|
2022-07-28 11:00:05 -04:00
|
|
|
if nodeInfo.V2ray.Inbounds[0].StreamSetting.TCPSettings != nil {
|
2022-06-01 13:35:41 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].StreamSetting.TCPSettings.AcceptProxyProtocol = config.EnableProxyProtocol
|
2022-06-02 13:22:56 -04:00
|
|
|
} else {
|
2022-07-28 11:00:05 -04:00
|
|
|
tcpSetting := &coreConf.TCPConfig{
|
2022-06-02 13:22:56 -04:00
|
|
|
AcceptProxyProtocol: config.EnableProxyProtocol,
|
2022-10-09 20:59:00 -04:00
|
|
|
} //Enable proxy protocol
|
2022-06-02 13:22:56 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].StreamSetting.TCPSettings = tcpSetting
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|
2022-06-02 13:22:56 -04:00
|
|
|
} else if *nodeInfo.V2ray.Inbounds[0].StreamSetting.Network == "ws" {
|
2022-07-28 11:00:05 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].StreamSetting.WSSettings = &coreConf.WebSocketConfig{
|
2022-10-09 20:59:00 -04:00
|
|
|
AcceptProxyProtocol: config.EnableProxyProtocol} //Enable proxy protocol
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|
|
|
|
// Build TLS and XTLS settings
|
|
|
|
if nodeInfo.EnableTls && config.CertConfig.CertMode != "none" {
|
|
|
|
nodeInfo.V2ray.Inbounds[0].StreamSetting.Security = nodeInfo.TLSType
|
|
|
|
certFile, keyFile, err := getCertFile(config.CertConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if nodeInfo.TLSType == "tls" {
|
2022-07-28 11:00:05 -04:00
|
|
|
tlsSettings := &coreConf.TLSConfig{
|
2022-06-01 13:35:41 -04:00
|
|
|
RejectUnknownSNI: config.CertConfig.RejectUnknownSni,
|
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
tlsSettings.Certs = append(tlsSettings.Certs, &coreConf.TLSCertConfig{CertFile: certFile, KeyFile: keyFile, OcspStapling: 3600})
|
2022-06-01 13:35:41 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].StreamSetting.TLSSettings = tlsSettings
|
|
|
|
} else if nodeInfo.TLSType == "xtls" {
|
2022-07-28 11:00:05 -04:00
|
|
|
xtlsSettings := &coreConf.XTLSConfig{
|
2022-06-01 13:35:41 -04:00
|
|
|
RejectUnknownSNI: config.CertConfig.RejectUnknownSni,
|
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
xtlsSettings.Certs = append(xtlsSettings.Certs, &coreConf.XTLSCertConfig{
|
2022-06-01 13:35:41 -04:00
|
|
|
CertFile: certFile,
|
|
|
|
KeyFile: keyFile,
|
|
|
|
OcspStapling: 3600})
|
|
|
|
nodeInfo.V2ray.Inbounds[0].StreamSetting.XTLSSettings = xtlsSettings
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Support ProxyProtocol for any transport protocol
|
|
|
|
if *nodeInfo.V2ray.Inbounds[0].StreamSetting.Network != "tcp" &&
|
|
|
|
*nodeInfo.V2ray.Inbounds[0].StreamSetting.Network != "ws" &&
|
|
|
|
config.EnableProxyProtocol {
|
2022-07-28 11:00:05 -04:00
|
|
|
sockoptConfig := &coreConf.SocketConfig{
|
2022-06-01 13:35:41 -04:00
|
|
|
AcceptProxyProtocol: config.EnableProxyProtocol,
|
2022-10-09 20:59:00 -04:00
|
|
|
} //Enable proxy protocol
|
2022-06-01 13:35:41 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].StreamSetting.SocketSettings = sockoptConfig
|
|
|
|
}
|
2022-06-02 13:22:56 -04:00
|
|
|
nodeInfo.V2ray.Inbounds[0].Settings = &setting
|
|
|
|
nodeInfo.V2ray.Inbounds[0].Tag = tag
|
2022-06-01 13:35:41 -04:00
|
|
|
return nodeInfo.V2ray.Inbounds[0].Build()
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
func getCertFile(certConfig *conf.CertConfig) (certFile string, keyFile string, err error) {
|
2022-06-01 13:35:41 -04:00
|
|
|
if certConfig.CertMode == "file" {
|
|
|
|
if certConfig.CertFile == "" || certConfig.KeyFile == "" {
|
|
|
|
return "", "", fmt.Errorf("cert file path or key file path not exist")
|
|
|
|
}
|
|
|
|
return certConfig.CertFile, certConfig.KeyFile, nil
|
|
|
|
} else if certConfig.CertMode == "dns" {
|
2022-06-04 05:12:28 -04:00
|
|
|
lego, err := legoCmd.New()
|
2022-06-01 13:35:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
certPath, keyPath, err := lego.DNSCert(certConfig.CertDomain, certConfig.Email, certConfig.Provider, certConfig.DNSEnv)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return certPath, keyPath, err
|
|
|
|
} else if certConfig.CertMode == "http" {
|
2022-06-04 05:12:28 -04:00
|
|
|
lego, err := legoCmd.New()
|
2022-06-01 13:35:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
certPath, keyPath, err := lego.HTTPCert(certConfig.CertDomain, certConfig.Email)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return certPath, keyPath, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", "", fmt.Errorf("unsupported certmode: %s", certConfig.CertMode)
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
func buildVlessFallbacks(fallbackConfigs []*conf.FallBackConfig) ([]*coreConf.VLessInboundFallback, error) {
|
2022-06-01 13:35:41 -04:00
|
|
|
if fallbackConfigs == nil {
|
|
|
|
return nil, fmt.Errorf("you must provide FallBackConfigs")
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
vlessFallBacks := make([]*coreConf.VLessInboundFallback, len(fallbackConfigs))
|
2022-06-01 13:35:41 -04:00
|
|
|
for i, c := range fallbackConfigs {
|
|
|
|
|
|
|
|
if c.Dest == "" {
|
|
|
|
return nil, fmt.Errorf("dest is required for fallback fialed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var dest json.RawMessage
|
|
|
|
dest, err := json.Marshal(c.Dest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("marshal dest %s config fialed: %s", dest, err)
|
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
vlessFallBacks[i] = &coreConf.VLessInboundFallback{
|
2022-06-01 13:35:41 -04:00
|
|
|
Name: c.SNI,
|
|
|
|
Alpn: c.Alpn,
|
|
|
|
Path: c.Path,
|
|
|
|
Dest: dest,
|
|
|
|
Xver: c.ProxyProtocolVer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vlessFallBacks, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
func buildTrojanFallbacks(fallbackConfigs []*conf.FallBackConfig) ([]*coreConf.TrojanInboundFallback, error) {
|
2022-06-01 13:35:41 -04:00
|
|
|
if fallbackConfigs == nil {
|
|
|
|
return nil, fmt.Errorf("you must provide FallBackConfigs")
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
trojanFallBacks := make([]*coreConf.TrojanInboundFallback, len(fallbackConfigs))
|
2022-06-01 13:35:41 -04:00
|
|
|
for i, c := range fallbackConfigs {
|
|
|
|
|
|
|
|
if c.Dest == "" {
|
|
|
|
return nil, fmt.Errorf("dest is required for fallback fialed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var dest json.RawMessage
|
|
|
|
dest, err := json.Marshal(c.Dest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("marshal dest %s config fialed: %s", dest, err)
|
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
trojanFallBacks[i] = &coreConf.TrojanInboundFallback{
|
2022-06-01 13:35:41 -04:00
|
|
|
Name: c.SNI,
|
|
|
|
Alpn: c.Alpn,
|
|
|
|
Path: c.Path,
|
|
|
|
Dest: dest,
|
|
|
|
Xver: c.ProxyProtocolVer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trojanFallBacks, nil
|
|
|
|
}
|