-
Notifications
You must be signed in to change notification settings - Fork 18
/
v2ray_api.go
117 lines (105 loc) · 3.42 KB
/
v2ray_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package v2raypool
import (
"context"
"fmt"
"strings"
"time"
"github.com/golang/protobuf/proto"
"github.com/iotames/v2raypool/v2rayapi"
v5 "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/app/proxyman"
pros "github.com/v2fly/v2ray-core/v5/app/proxyman/command"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/serial"
"github.com/v2fly/v2ray-core/v5/proxy/http"
"github.com/v2fly/v2ray-core/v5/proxy/socks"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type V2rayApiClient struct {
addr string
conn *grpc.ClientConn
c pros.HandlerServiceClient
ctx context.Context
cancel context.CancelFunc
}
func NewV2rayApiClientV5(addr string) *V2rayApiClient {
return &V2rayApiClient{addr: addr}
}
func (a *V2rayApiClient) Close() {
a.cancel()
a.conn.Close()
}
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)
// defer conn.Close()
a.ctx, a.cancel = context.WithTimeout(context.Background(), time.Second*10)
// defer cancel()
return nil
}
// AddInbound 添加入站规则
// protocol http|socks
func (a V2rayApiClient) AddInbound(inport net.Port, intag, protocol string) error {
var proxySet proto.Message
protocol = strings.ToLower(protocol)
if protocol == "http" {
proxySet = &http.ServerConfig{
AllowTransparent: false,
Timeout: 30,
// UserLevel: 0,
}
}
if protocol == "socks" {
proxySet = &socks.ServerConfig{
AuthType: socks.AuthType_NO_AUTH,
UdpEnabled: true,
// Address: net.NewIPOrDomain(net.AnyIP),
// UserLevel: 0,
}
}
resp, err := a.c.AddInbound(a.ctx, &pros.AddInboundRequest{Inbound: &v5.InboundHandlerConfig{
Tag: intag,
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(inport),
Listen: net.NewIPOrDomain(net.AnyIP),
}),
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
}
func (a V2rayApiClient) RemoveInbound(intag string) error {
_, err := a.c.RemoveInbound(a.ctx, &pros.RemoveInboundRequest{Tag: intag})
if err != nil {
fmt.Printf("---RemoveInbound(%s)--err(%v)-\n", intag, err)
}
return err
}
func (a V2rayApiClient) AddOutboundByV2rayNode(nd V2rayNode, outag string) error {
var reqs []*pros.AddOutboundRequest
var resp *pros.AddOutboundResponse
var err error
reqs, err = v2rayapi.GetOutboundRequest(nd.Port, nd.Aid, nd.Protocol, nd.Add, nd.Host, nd.Id, nd.Net, nd.Path, nd.Tls, nd.Type, outag) // nd, outag
if err != nil {
return err
}
for _, req := range reqs {
resp, err = a.c.AddOutbound(a.ctx, req)
fmt.Printf("---AddOutbound--(%s://)(%s)--(%s:%v)--result(%s)--err(%v)--\n", nd.Protocol, outag, nd.Add, nd.Port, resp, err)
}
return err
}
func (a V2rayApiClient) RemoveOutbound(outag string) error {
_, err := a.c.RemoveOutbound(a.ctx, &pros.RemoveOutboundRequest{Tag: outag})
if err != nil {
fmt.Printf("---RemoveOutbound(%s)--err(%v)-\n", outag, err)
}
return err
}