forked from MatsuriDayo/Matsuri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2ray.go
167 lines (143 loc) · 4.24 KB
/
v2ray.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package libcore
import (
"context"
"fmt"
"libcore/device"
"log"
"strings"
"sync"
"sync/atomic"
"unsafe"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/app/dispatcher"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net"
dns_feature "github.com/v2fly/v2ray-core/v5/features/dns"
"github.com/v2fly/v2ray-core/v5/features/routing"
"github.com/v2fly/v2ray-core/v5/features/stats"
"github.com/v2fly/v2ray-core/v5/infra/conf/serial"
"github.com/v2fly/v2ray-core/v5/nekoutils"
)
func GetV2RayVersion() string {
return core.Version() + "-喵"
}
type V2RayInstance struct {
access sync.Mutex
started bool
Core *core.Instance
StatsManager stats.Manager
Dispatcher *dispatcher.DefaultDispatcher
DnsClient dns_feature.Client
ForTest bool
}
func NewV2rayInstance() *V2RayInstance {
return &V2RayInstance{}
}
func (instance *V2RayInstance) LoadConfig(content string) error {
defer device.DeferPanicToError("LoadConfig", func(err error) { log.Println(err) })
instance.access.Lock()
defer instance.access.Unlock()
// load v4 or v5 config
var config *core.Config
var err error
if content2 := strings.TrimPrefix(content, "matsuri-v2ray-v5"); content2 != content {
config, err = core.LoadConfig("jsonv5", strings.NewReader(content2))
if err != nil {
log.Println(content, err.Error())
return err
}
} else {
config, err = serial.LoadJSONConfig(strings.NewReader(content))
if err != nil {
log.Println(content, err.Error())
return err
}
}
c, err := core.New(config)
if err != nil {
return err
}
instance.Core = c
instance.StatsManager = c.GetFeature(stats.ManagerType()).(stats.Manager)
instance.Dispatcher = c.GetFeature(routing.DispatcherType()).(routing.Dispatcher).(*dispatcher.DefaultDispatcher)
instance.DnsClient = c.GetFeature(dns_feature.ClientType()).(dns_feature.Client)
if !instance.ForTest {
atomic.StorePointer(&v2rayDNSClient, unsafe.Pointer(&instance.DnsClient))
}
return nil
}
func (instance *V2RayInstance) Start() error {
defer device.DeferPanicToError("Start", func(err error) { log.Println(err) })
instance.access.Lock()
defer instance.access.Unlock()
if instance.started {
return newError("already started")
}
if instance.Core == nil {
return newError("not initialized")
}
err := instance.Core.Start()
if err != nil {
return err
}
instance.started = true
return nil
}
func (instance *V2RayInstance) QueryStats(tag string, direct string) int64 {
if instance.StatsManager == nil {
return 0
}
counter := instance.StatsManager.GetCounter(fmt.Sprintf("outbound>>>%s>>>traffic>>>%s", tag, direct))
if counter == nil {
return 0
}
return counter.Set(0)
}
func (instance *V2RayInstance) Close() error {
defer device.DeferPanicToError("Close", func(err error) { log.Println(err) })
instance.access.Lock()
defer instance.access.Unlock()
if instance.started {
if !instance.ForTest {
atomic.StorePointer(&v2rayDNSClient, unsafe.Pointer(nil))
}
nekoutils.ResetConnections(uintptr(unsafe.Pointer(instance.Core)))
return instance.Core.Close()
}
return nil
}
func (instance *V2RayInstance) DialContext(ctx context.Context, destination net.Destination) (net.Conn, error) {
ctx = core.WithContext(ctx, instance.Core)
r, err := instance.Dispatcher.Dispatch(ctx, destination)
if err != nil {
return nil, err
}
var readerOpt buf.ConnectionOption
if destination.Network == net.Network_TCP {
readerOpt = buf.ConnectionOutputMulti(r.Reader)
} else {
readerOpt = buf.ConnectionOutputMultiUDP(r.Reader)
}
return buf.NewConnection(buf.ConnectionInputMulti(r.Writer), readerOpt), nil
}
// Neko connections
func (instance *V2RayInstance) SetConnectionPoolEnabled(enable bool) {
nekoutils.SetConnectionPoolV2RayEnabled(uintptr(unsafe.Pointer(instance.Core)), enable)
nekoutils.ResetAllConnections(false)
}
func ResetAllConnections(system bool) {
nekoutils.ResetAllConnections(system)
}
func ListV2rayConnections() string {
return nekoutils.ListConnections(0)
}
func CloseV2rayConnection(id uint32) {
m := &nekoutils.ConnectionPool_V2Ray.Map
m.Range(func(key interface{}, value interface{}) bool {
if c, ok := key.(*nekoutils.ManagedV2rayConn); ok && c.ID() == id {
c.Close()
return false
}
return true
})
}