Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
initial support for hysteria
update config
update api
change user email format
...
  • Loading branch information
Yuzuki616 committed Jun 8, 2023
1 parent 52f9734 commit 42bb7bc
Show file tree
Hide file tree
Showing 30 changed files with 807 additions and 981 deletions.
131 changes: 104 additions & 27 deletions api/panel/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package panel

import (
"fmt"
"github.com/goccy/go-json"
"reflect"
"regexp"
Expand All @@ -9,21 +10,12 @@ import (
"time"
)

type NodeInfo struct {
NodeId int `json:"-"`
NodeType string `json:"-"`
Rules []*regexp.Regexp `json:"-"`
Host string `json:"host"`
ServerPort int `json:"server_port"`
ServerName string `json:"server_name"`
Network string `json:"network"`
NetworkSettings json.RawMessage `json:"networkSettings"`
Cipher string `json:"cipher"`
ServerKey string `json:"server_key"`
Tls int `json:"tls"`
Routes []Route `json:"routes"`
BaseConfig *BaseConfig `json:"base_config"`
type CommonNodeRsp struct {
ServerPort int `json:"server_port"`
Routes []Route `json:"routes"`
BaseConfig BaseConfig `json:"base_config"`
}

type Route struct {
Id int `json:"id"`
Match interface{} `json:"match"`
Expand All @@ -35,37 +27,122 @@ type BaseConfig struct {
PullInterval any `json:"pull_interval"`
}

func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {
type V2rayNodeRsp struct {
Tls int `json:"tls"`
Network string `json:"network"`
NetworkSettings json.RawMessage `json:"networkSettings"`
ServerName string `json:"server_name"`
}

type ShadowsocksNodeRsp struct {
Cipher string `json:"cipher"`
ServerKey string `json:"server_key"`
}

type TrojanNodeRsp struct {
Host string `json:"host"`
ServerName string `json:"server_name"`
}

type HysteriaNodeRsp struct {
UpMbps int `json:"up_mbps"`
DownMbps int `json:"down_mbps"`
Obfs string `json:"obfs"`
}

type NodeInfo struct {
Id int
Type string
Rules []*regexp.Regexp
Port int
Network string
NetworkSettings json.RawMessage
Tls bool
Host string
ServerName string
UpMbps int
DownMbps int
ServerKey string
Cipher string
HyObfs string
PushInterval time.Duration
PullInterval time.Duration
}

func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
const path = "/api/v1/server/UniProxy/config"
r, err := c.client.R().Get(path)
if err = c.checkResponse(r, path, err); err != nil {
return
}
err = json.Unmarshal(r.Body(), &nodeInfo)
err = json.Unmarshal(r.Body(), &node)
if err != nil {
return
}
if c.etag == r.Header().Get("ETag") { // node info not changed
return nil, nil
}
nodeInfo.NodeId = c.NodeId
nodeInfo.NodeType = c.NodeType
for i := range nodeInfo.Routes { // parse rules from routes
if nodeInfo.Routes[i].Action == "block" {
// parse common params
node.Id = c.NodeId
node.Type = c.NodeType
common := CommonNodeRsp{}
err = json.Unmarshal(r.Body(), &common)
if err != nil {
return nil, fmt.Errorf("decode common params error: %s", err)
}
for i := range common.Routes { // parse rules from routes
if common.Routes[i].Action == "block" {
var matchs []string
if _, ok := nodeInfo.Routes[i].Match.(string); ok {
matchs = strings.Split(nodeInfo.Routes[i].Match.(string), ",")
if _, ok := common.Routes[i].Match.(string); ok {
matchs = strings.Split(common.Routes[i].Match.(string), ",")
} else {
matchs = nodeInfo.Routes[i].Match.([]string)
matchs = common.Routes[i].Match.([]string)
}
for _, v := range matchs {
nodeInfo.Rules = append(nodeInfo.Rules, regexp.MustCompile(v))
node.Rules = append(node.Rules, regexp.MustCompile(v))
}
}
}
nodeInfo.Routes = nil
nodeInfo.BaseConfig.PullInterval = intervalToTime(nodeInfo.BaseConfig.PullInterval)
nodeInfo.BaseConfig.PushInterval = intervalToTime(nodeInfo.BaseConfig.PushInterval)
node.PullInterval = intervalToTime(common.BaseConfig.PullInterval)
node.PushInterval = intervalToTime(common.BaseConfig.PushInterval)
// parse protocol params
switch c.NodeType {
case "v2ray":
rsp := V2rayNodeRsp{}
err = json.Unmarshal(r.Body(), &rsp)
if err != nil {
return nil, fmt.Errorf("decode v2ray params error: %s", err)
}
node.Network = rsp.Network
node.NetworkSettings = rsp.NetworkSettings
node.ServerName = rsp.ServerName
if rsp.Tls == 1 {
node.Tls = true
}
case "shadowsocks":
rsp := ShadowsocksNodeRsp{}
err = json.Unmarshal(r.Body(), &rsp)
if err != nil {
return nil, fmt.Errorf("decode v2ray params error: %s", err)
}
node.ServerKey = rsp.ServerKey
node.Cipher = rsp.Cipher
case "trojan":
rsp := TrojanNodeRsp{}
err = json.Unmarshal(r.Body(), &rsp)
if err != nil {
return nil, fmt.Errorf("decode v2ray params error: %s", err)
}
case "hysteria":
rsp := HysteriaNodeRsp{}
err = json.Unmarshal(r.Body(), &rsp)
if err != nil {
return nil, fmt.Errorf("decode v2ray params error: %s", err)
}
node.DownMbps = rsp.DownMbps
node.UpMbps = rsp.UpMbps
node.HyObfs = rsp.Obfs
}
c.etag = r.Header().Get("Etag")
return
}
Expand Down
64 changes: 17 additions & 47 deletions common/builder/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"errors"
"fmt"
"github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/common/file"
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/node/lego"
"github.com/goccy/go-json"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/core"
Expand All @@ -23,22 +21,22 @@ func BuildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
t := coreConf.TransportProtocol(nodeInfo.Network)
in.StreamSetting = &coreConf.StreamConfig{Network: &t}
var err error
switch nodeInfo.NodeType {
switch nodeInfo.Type {
case "v2ray":
err = buildV2ray(config, nodeInfo, in)
case "trojan":
err = buildTrojan(config, nodeInfo, in)
case "shadowsocks":
err = buildShadowsocks(config, nodeInfo, in)
default:
return nil, fmt.Errorf("unsupported node type: %s, Only support: V2ray, Trojan, Shadowsocks", nodeInfo.NodeType)
return nil, fmt.Errorf("unsupported node type: %s, Only support: V2ray, Trojan, Shadowsocks", nodeInfo.Type)
}
if err != nil {
return nil, err
}
// Set server port
in.PortList = &coreConf.PortList{
Range: []coreConf.PortRange{{From: uint32(nodeInfo.ServerPort), To: uint32(nodeInfo.ServerPort)}},
Range: []coreConf.PortRange{{From: uint32(nodeInfo.Port), To: uint32(nodeInfo.Port)}},
}
// Set Listen IP address
ipAddress := net.ParseAddress(config.ListenIP)
Expand All @@ -48,7 +46,7 @@ func BuildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
Enabled: true,
DestOverride: &coreConf.StringList{"http", "tls"},
}
if config.DisableSniffing {
if config.XrayOptions.DisableSniffing {
sniffingConfig.Enabled = false
}
in.SniffingConfig = sniffingConfig
Expand All @@ -66,7 +64,7 @@ func BuildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
AcceptProxyProtocol: config.EnableProxyProtocol} //Enable proxy protocol
}
// Set TLS or Reality settings
if nodeInfo.Tls != 0 {
if nodeInfo.Tls {
if config.CertConfig == nil {
return nil, errors.New("the CertConfig is not vail")
}
Expand All @@ -91,16 +89,11 @@ func BuildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
}
default:
// Normal tls
in.StreamSetting.Security = "tls"
certFile, keyFile, err := getCertFile(config.CertConfig)
if err != nil {
return nil, err
}
in.StreamSetting.TLSSettings = &coreConf.TLSConfig{
Certs: []*coreConf.TLSCertConfig{
{
CertFile: certFile,
KeyFile: keyFile,
CertFile: config.CertConfig.CertFile,
KeyFile: config.CertConfig.KeyFile,
OcspStapling: 3600,
},
},
Expand All @@ -111,24 +104,25 @@ func BuildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
// Support ProxyProtocol for any transport protocol
if *in.StreamSetting.Network != "tcp" &&
*in.StreamSetting.Network != "ws" &&
*in.StreamSetting.Network != "" &&
config.EnableProxyProtocol {
sockoptConfig := &coreConf.SocketConfig{
socketConfig := &coreConf.SocketConfig{
AcceptProxyProtocol: config.EnableProxyProtocol,
TFO: config.EnableTFO,
TFO: config.XrayOptions.EnableTFO,
} //Enable proxy protocol
in.StreamSetting.SocketSettings = sockoptConfig
in.StreamSetting.SocketSettings = socketConfig
}
in.Tag = tag
return in.Build()
}

func buildV2ray(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
if config.EnableVless {
if config.XrayOptions.EnableVless {
//Set vless
inbound.Protocol = "vless"
if config.EnableFallback {
if config.XrayOptions.EnableFallback {
// Set fallback
fallbackConfigs, err := buildVlessFallbacks(config.FallBackConfigs)
fallbackConfigs, err := buildVlessFallbacks(config.XrayOptions.FallBackConfigs)
if err != nil {
return err
}
Expand Down Expand Up @@ -185,9 +179,9 @@ func buildV2ray(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound

func buildTrojan(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
inbound.Protocol = "trojan"
if config.EnableFallback {
if config.XrayOptions.EnableFallback {
// Set fallback
fallbackConfigs, err := buildTrojanFallbacks(config.FallBackConfigs)
fallbackConfigs, err := buildTrojanFallbacks(config.XrayOptions.FallBackConfigs)
if err != nil {
return err
}
Expand Down Expand Up @@ -234,7 +228,7 @@ func buildShadowsocks(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, i
settings.Users = append(settings.Users, defaultSSuser)
settings.NetworkList = &coreConf.NetworkList{"tcp", "udp"}
settings.IVCheck = true
if config.DisableIVCheck {
if config.XrayOptions.DisableIVCheck {
settings.IVCheck = false
}
t := coreConf.TransportProtocol("tcp")
Expand All @@ -247,30 +241,6 @@ func buildShadowsocks(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, i
return nil
}

func getCertFile(certConfig *conf.CertConfig) (certFile string, keyFile string, err error) {
if certConfig.CertFile == "" || certConfig.KeyFile == "" {
return "", "", fmt.Errorf("cert file path or key file path not exist")
}
switch certConfig.CertMode {
case "file":
return certConfig.CertFile, certConfig.KeyFile, nil
case "dns", "http":
if file.IsExist(certConfig.CertFile) && file.IsExist(certConfig.KeyFile) {
return certConfig.CertFile, certConfig.KeyFile, nil
}
l, err := lego.New(certConfig)
if err != nil {
return "", "", fmt.Errorf("create lego object error: %s", err)
}
err = l.CreateCert()
if err != nil {
return "", "", fmt.Errorf("create cert error: %s", err)
}
return certConfig.CertFile, certConfig.KeyFile, nil
}
return "", "", fmt.Errorf("unsupported certmode: %s", certConfig.CertMode)
}

func buildVlessFallbacks(fallbackConfigs []conf.FallBackConfig) ([]*coreConf.VLessInboundFallback, error) {
if fallbackConfigs == nil {
return nil, fmt.Errorf("you must provide FallBackConfigs")
Expand Down
11 changes: 5 additions & 6 deletions common/builder/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package builder

import (
"fmt"
"github.com/Yuzuki616/V2bX/api/panel"
conf2 "github.com/Yuzuki616/V2bX/conf"
"github.com/goccy/go-json"
"github.com/xtls/xray-core/common/net"
Expand All @@ -11,7 +10,7 @@ import (
)

// BuildOutbound build freedom outbund config for addoutbound
func BuildOutbound(config *conf2.ControllerConfig, nodeInfo *panel.NodeInfo, tag string) (*core.OutboundHandlerConfig, error) {
func BuildOutbound(config *conf2.ControllerConfig, tag string) (*core.OutboundHandlerConfig, error) {
outboundDetourConfig := &conf.OutboundDetourConfig{}
outboundDetourConfig.Protocol = "freedom"
outboundDetourConfig.Tag = tag
Expand All @@ -24,9 +23,9 @@ func BuildOutbound(config *conf2.ControllerConfig, nodeInfo *panel.NodeInfo, tag

// Freedom Protocol setting
var domainStrategy = "Asis"
if config.EnableDNS {
if config.DNSType != "" {
domainStrategy = config.DNSType
if config.XrayOptions.EnableDNS {
if config.XrayOptions.DNSType != "" {
domainStrategy = config.XrayOptions.DNSType
} else {
domainStrategy = "UseIP"
}
Expand All @@ -37,7 +36,7 @@ func BuildOutbound(config *conf2.ControllerConfig, nodeInfo *panel.NodeInfo, tag
var setting json.RawMessage
setting, err := json.Marshal(proxySetting)
if err != nil {
return nil, fmt.Errorf("marshal proxy %s config fialed: %s", nodeInfo.NodeType, err)
return nil, fmt.Errorf("marshal proxy config error: %s", err)
}
outboundDetourConfig.Settings = &setting
return outboundDetourConfig.Build()
Expand Down
Loading

0 comments on commit 42bb7bc

Please sign in to comment.