Skip to content

Commit

Permalink
FixedBug: Read v2ray config err from Stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
iotames committed Apr 9, 2024
1 parent b4e66ae commit e043c9f
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 107 deletions.
15 changes: 13 additions & 2 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ func main() {
flag.Parse()
var err error
c := vp.NewV2rayApiClientV5(saddr)
if c.Dial() == nil {
err = c.Dial()
if err == nil {
defer c.Close()
} else {
panic(err)
}
if addin != "" {
args := strings.Split(addin, ",")
Expand Down Expand Up @@ -78,7 +81,15 @@ func main() {
}
nett = "ws"
fmt.Println("-----", addr, port, nett, id, tls, outag)
err = c.AddOutbound(addr, port, nett, id, tls, outag)
nd := vp.V2rayNode{
Protocol: "vmess",
Add: addr,
Port: port,
Net: nett,
Id: id,
Tls: tls,
}
err = c.AddOutboundByV2rayNode(nd, outag)
if err != nil {
panic(err)
}
Expand Down
30 changes: 15 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
module github.com/iotames/v2raypool

go 1.19
go 1.21

require (
github.com/golang/protobuf v1.5.3
github.com/iotames/glayui v1.0.2
github.com/iotames/miniutils v1.0.10
github.com/golang/protobuf v1.5.4
github.com/iotames/glayui v1.1.2
github.com/iotames/miniutils v1.0.11
github.com/joho/godotenv v1.5.1
github.com/v2fly/v2ray-core/v5 v5.7.0
google.golang.org/grpc v1.58.1
google.golang.org/protobuf v1.31.0
github.com/v2fly/v2ray-core/v5 v5.15.1
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.33.0
)

require (
github.com/adrg/xdg v0.4.0 // indirect
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/pires/go-proxyproto v0.6.2 // indirect
github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/pires/go-proxyproto v0.7.0 // indirect
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 // indirect
github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb // indirect
github.com/v2fly/ss-bloomring v0.0.0-20210312155135-28617310f63e // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
)
184 changes: 120 additions & 64 deletions go.sum

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ func RunServer() {

func RunProxyPoolGrpcServer() {
lisAddr := fmt.Sprintf(":%d", conf.GetConf().GrpcPort)
fmt.Printf("---listenAddr(%s)---\n", lisAddr)
lis, err := net.Listen("tcp", lisAddr)
if err != nil {
fmt.Printf("failed to listen: %v", err)
Expand All @@ -281,8 +280,10 @@ func RunProxyPoolGrpcServer() {
s := grpc.NewServer()
g.RegisterProxyPoolServiceServer(s, ProxyPoolServer{})

fmt.Printf("SUCCESS: gRPC Server Listening At(%+v)\n", lis.Addr())
if err := s.Serve(lis); err != nil {
fmt.Printf("failed to serve: %v", err)
err = fmt.Errorf("failed to start grpc server: %v", err)
panic(err)
} else {
fmt.Printf("SUCCESS: gRPC Server Listening At(%+v)\n", lis.Addr())
}
}
25 changes: 15 additions & 10 deletions v2ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,24 @@ func (v *V2rayServer) setExeCmd(configFile string) {
// https://www.v2fly.org/v5/config/overview.html
// 默认为v4配置格式。添加命令参数 "-format", "jsonv5" 后,才是v5的配置
if configFile == "" {
v.cmd = exec.Command(v.v2rayPath, "run")
v.jconf = getV2rayConfigV4(v.selectNode, v.localPort)
v.cmd.Stdin = v.jconf.Reader()
if v.localPort > 0 {
configFile = V2RAY_CONFIG_FILE
} else {
configFile = V2RAYPOOL_CONFIG_FILE
}
err := v.jconf.SaveToFile(configFile)
if err != nil {
fmt.Printf("--------V2rayServer.setExeCmd()--jsonFileSaveFail(%v)--", err)
}
// v5.7使用标准输入读取配置正常,v5.14.1则出现BUG。故一律使用配置文件读取配置
// v.cmd = exec.Command(v.v2rayPath, "run")
// v.cmd.Stdin = v.jconf.Reader()
} else {
v.cmd = exec.Command(v.v2rayPath, "run", "-c", configFile)
v.jconf = NewJsonConfigFromFile(configFile)
}
// v5.7使用标准输入读取配置正常,v5.14.1则出现BUG。故一律使用配置文件读取配置
v.cmd = exec.Command(v.v2rayPath, "run", "-c", configFile)
// cmd.Stdout = os.Stdout
v.cmd.Stderr = os.Stderr
}
Expand All @@ -76,13 +87,7 @@ func (v *V2rayServer) Start(configFile string) error {
v.setExeCmd(configFile)
err := v.cmd.Start()
if err == nil {
if v.localPort > 0 {
err1 := v.jconf.SaveToFile(V2RAY_CONFIG_FILE)
if err1 != nil {
fmt.Printf("--------V2rayServer.Start()--jsonFileSaveFail(%v)--", err1)
}
}
fmt.Printf("----v2ray-Start(%s)--Pid(%d)---\n", configFile, v.cmd.Process.Pid)
fmt.Printf("--SUCCESS--Start(%s)-v2ray-Pid(%d)---\n", configFile, v.cmd.Process.Pid)
}
return err
}
14 changes: 2 additions & 12 deletions v2ray_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (a *V2rayApiClient) Dial() error {
var err error
a.conn, err = grpc.Dial(a.addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Printf("------V2rayApiClient--Dial(%v)--\n", err)
return err
}
a.c = pros.NewHandlerServiceClient(a.conn)
Expand Down Expand Up @@ -90,6 +91,7 @@ func (a V2rayApiClient) AddInbound(inport net.Port, intag, protocol string) erro
}),
ProxySettings: serial.ToTypedMessage(proxySet),
}})
// rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing: dial tcp 127.0.0.1:15491: connectex: No connection could be made because the target machine actively refused it."
fmt.Printf("---AddInbound(%s)--port(%d)--result(%s)--err(%v)--\n", intag, inport, resp, err)
return err
}
Expand Down Expand Up @@ -190,18 +192,6 @@ func (a V2rayApiClient) AddOutboundByV2rayNode(nd V2rayNode, outag string) error
return err
}

func (a V2rayApiClient) AddOutbound(addr, port, nett, id, tls, outag string) error {
nd := V2rayNode{
Protocol: "vmess",
Add: addr,
Port: port,
Net: nett,
Id: id,
Tls: tls,
}
return a.AddOutboundByV2rayNode(nd, outag)
}

func (a V2rayApiClient) RemoveOutbound(outag string) error {
result, err := a.c.RemoveOutbound(a.ctx, &pros.RemoveOutboundRequest{Tag: outag})
fmt.Printf("---RemoveOutbound(%s)----result(%v)---err(%v)-\n", outag, result, err)
Expand Down
2 changes: 2 additions & 0 deletions v2ray_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const ROUTING_RULES_FILE = "routing.rules.json"
// 只写文件,被 ROUTING_RULES_FILE 文件覆盖部分值(v2raypool)
const V2RAY_CONFIG_FILE = "v2ray.config.json"

const V2RAYPOOL_CONFIG_FILE = "v2raypool.config.json"

// github.com/v2fly/v2ray-core/v5/infra/conf/v5cfg
// 执行 ./v2ray run -c $configure_file_name -format jsonv5 命令以运行您的配置文件。

Expand Down
2 changes: 1 addition & 1 deletion v2ray_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ParseV2rayNodes(data string) []V2rayNode {

func parseNodeInfo(d string) (nd V2rayNode, err error) {
ninfo := strings.Split(d, "://")
if len(ninfo) == 2 {
if len(ninfo) > 1 {
var b []byte
nd.Protocol = ninfo[0]
if nd.Protocol == "vmess" {
Expand Down

0 comments on commit e043c9f

Please sign in to comment.