mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
修订代码,进一步将cmd代码移动到machine中
This commit is contained in:
@@ -288,12 +288,12 @@ func generateConfigFileInteractively(m *machine.M) {
|
||||
switch ihot {
|
||||
case 0:
|
||||
|
||||
m.HotLoadDialConf("", confServer.Dial)
|
||||
m.HotLoadListenConf(confServer.Listen)
|
||||
m.LoadDialConf(confServer.Dial)
|
||||
m.LoadListenConf(confServer.Listen, true)
|
||||
|
||||
case 1:
|
||||
m.HotLoadDialConf("", confClient.Dial)
|
||||
m.HotLoadListenConf(confClient.Listen)
|
||||
m.LoadDialConf(confClient.Dial)
|
||||
m.LoadListenConf(confClient.Listen, true)
|
||||
}
|
||||
|
||||
utils.PrintStr("加载成功!你可以回退(ctrl+c)到上级来使用 【查询当前状态】来查询新增的配置\n")
|
||||
@@ -525,12 +525,12 @@ func interactively_hotLoadConfigFile(m *machine.M) {
|
||||
switch confMode {
|
||||
case proxy.StandardMode:
|
||||
if len(standardConf.Dial) > 0 {
|
||||
defaultMachine.HotLoadDialConf("", standardConf.Dial)
|
||||
defaultMachine.LoadDialConf(standardConf.Dial)
|
||||
|
||||
}
|
||||
|
||||
if len(standardConf.Listen) > 0 {
|
||||
defaultMachine.HotLoadListenConf(standardConf.Listen)
|
||||
defaultMachine.LoadListenConf(standardConf.Listen, true)
|
||||
|
||||
}
|
||||
case proxy.SimpleMode:
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
"github.com/pkg/profile"
|
||||
"go.uber.org/zap"
|
||||
|
||||
vs "github.com/e1732a364fed/v2ray_simple"
|
||||
"github.com/e1732a364fed/v2ray_simple/httpLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/machine"
|
||||
"github.com/e1732a364fed/v2ray_simple/netLayer"
|
||||
@@ -108,7 +107,7 @@ func mainFunc() (result int) {
|
||||
|
||||
result = -3
|
||||
|
||||
defaultMachine.Cleanup()
|
||||
defaultMachine.Stop()
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -231,20 +230,14 @@ func mainFunc() (result int) {
|
||||
fmt.Printf("Log Level:%d\n", utils.LogLevel)
|
||||
|
||||
if ce := utils.CanLogInfo("Options"); ce != nil {
|
||||
|
||||
ce.Write(
|
||||
zap.String("Log Level", utils.LogLevelStr(utils.LogLevel)),
|
||||
zap.Bool("UseReadv", netLayer.UseReadv),
|
||||
)
|
||||
|
||||
} else {
|
||||
|
||||
fmt.Printf("UseReadv:%t\n", netLayer.UseReadv)
|
||||
|
||||
}
|
||||
|
||||
var Default_uuid string
|
||||
|
||||
if mainFallback != nil {
|
||||
defaultMachine.RoutingEnv.MainFallback = mainFallback
|
||||
}
|
||||
@@ -252,17 +245,15 @@ func mainFunc() (result int) {
|
||||
//load inServers and RoutingEnv
|
||||
switch configMode {
|
||||
case proxy.SimpleMode:
|
||||
//var theServer proxy.Server
|
||||
result, _ = defaultMachine.LoadSimpleServer(simpleConf)
|
||||
if result < 0 {
|
||||
return result
|
||||
}
|
||||
//allServers = append(allServers, theServer) //loadSimpleServer 已经加过一遍了
|
||||
|
||||
case proxy.StandardMode:
|
||||
|
||||
if appConf != nil {
|
||||
Default_uuid = appConf.DefaultUUID
|
||||
defaultMachine.DefaultUUID = appConf.DefaultUUID
|
||||
}
|
||||
|
||||
//虽然标准模式支持多个Server,目前先只考虑一个
|
||||
@@ -273,42 +264,10 @@ func mainFunc() (result int) {
|
||||
break
|
||||
}
|
||||
|
||||
for _, serverConf := range standardConf.Listen {
|
||||
thisConf := serverConf
|
||||
|
||||
if thisConf.Uuid == "" && Default_uuid != "" {
|
||||
thisConf.Uuid = Default_uuid
|
||||
}
|
||||
|
||||
thisServer, err := proxy.NewServer(thisConf)
|
||||
if err != nil {
|
||||
if ce := utils.CanLogErr("can not create local server:"); ce != nil {
|
||||
ce.Write(zap.Error(err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
defaultMachine.AllServers = append(defaultMachine.AllServers, thisServer)
|
||||
}
|
||||
|
||||
//将@前缀的 回落dest配置 替换成 实际的 地址。
|
||||
defaultMachine.LoadListenConf(standardConf.Listen, false)
|
||||
|
||||
if len(standardConf.Fallbacks) > 0 {
|
||||
for _, fbConf := range standardConf.Fallbacks {
|
||||
if fbConf.Dest == nil {
|
||||
continue
|
||||
}
|
||||
if deststr, ok := fbConf.Dest.(string); ok && strings.HasPrefix(deststr, "@") {
|
||||
for _, s := range defaultMachine.AllServers {
|
||||
if s.GetTag() == deststr[1:] {
|
||||
log.Println("got tag fallback dest, will set to ", s.AddrStr())
|
||||
fbConf.Dest = s.AddrStr()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
defaultMachine.ParseFallbacksAtSymbol(standardConf.Fallbacks)
|
||||
}
|
||||
var myCountryISO_3166 string
|
||||
if appConf != nil {
|
||||
@@ -331,31 +290,18 @@ func mainFunc() (result int) {
|
||||
if len(standardConf.Dial) < 1 {
|
||||
utils.Warn("no dial in config settings, will add 'direct'")
|
||||
|
||||
defaultMachine.AllClients = append(defaultMachine.AllClients, vs.DirectClient)
|
||||
defaultMachine.DefaultOutClient = vs.DirectClient
|
||||
|
||||
defaultMachine.RoutingEnv.SetClient("direct", vs.DirectClient)
|
||||
defaultMachine.SetDefaultDirectClient()
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
defaultMachine.HotLoadDialConf(Default_uuid, standardConf.Dial)
|
||||
defaultMachine.LoadDialConf(standardConf.Dial)
|
||||
|
||||
}
|
||||
|
||||
runPreCommands()
|
||||
|
||||
if (defaultMachine.DefaultOutClient != nil) && (len(defaultMachine.AllServers) > 0) {
|
||||
|
||||
for _, inServer := range defaultMachine.AllServers {
|
||||
lis := vs.ListenSer(inServer, defaultMachine.DefaultOutClient, &defaultMachine.RoutingEnv, &defaultMachine.GlobalInfo)
|
||||
|
||||
if lis != nil {
|
||||
defaultMachine.ListenCloserList = append(defaultMachine.ListenCloserList, lis)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
defaultMachine.Start()
|
||||
|
||||
//没可用的listen/dial,而且还无法动态更改配置
|
||||
if NoFuture(defaultMachine) {
|
||||
@@ -409,7 +355,7 @@ func mainFunc() (result int) {
|
||||
|
||||
utils.Info("Program got close signal.")
|
||||
|
||||
defaultMachine.Cleanup()
|
||||
defaultMachine.Stop()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package machine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/e1732a364fed/v2ray_simple"
|
||||
"github.com/e1732a364fed/v2ray_simple/netLayer"
|
||||
@@ -11,13 +10,13 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (m *M) HotLoadDialConf(Default_uuid string, conf []*proxy.DialConf) (ok bool) {
|
||||
func (m *M) LoadDialConf(conf []*proxy.DialConf) (ok bool) {
|
||||
ok = true
|
||||
|
||||
for _, d := range conf {
|
||||
|
||||
if d.Uuid == "" && Default_uuid != "" {
|
||||
d.Uuid = Default_uuid
|
||||
if d.Uuid == "" && m.DefaultUUID != "" {
|
||||
d.Uuid = m.DefaultUUID
|
||||
}
|
||||
|
||||
outClient, err := proxy.NewClient(d)
|
||||
@@ -49,34 +48,47 @@ func (m *M) HotLoadDialConf(Default_uuid string, conf []*proxy.DialConf) (ok boo
|
||||
|
||||
}
|
||||
|
||||
func (m *M) HotLoadListenConf(conf []*proxy.ListenConf) (ok bool) {
|
||||
// add; when hot=true, listen the server
|
||||
func (m *M) LoadListenConf(conf []*proxy.ListenConf, hot bool) (ok bool) {
|
||||
ok = true
|
||||
|
||||
if m.DefaultOutClient == nil {
|
||||
m.DefaultOutClient = v2ray_simple.DirectClient
|
||||
}
|
||||
|
||||
for i, l := range conf {
|
||||
for _, l := range conf {
|
||||
if l.Uuid == "" && m.DefaultUUID != "" {
|
||||
l.Uuid = m.DefaultUUID
|
||||
}
|
||||
|
||||
inServer, err := proxy.NewServer(l)
|
||||
if err != nil {
|
||||
log.Println("can not create inServer: ", i, err)
|
||||
|
||||
if ce := utils.CanLogErr("Can not create listen server"); ce != nil {
|
||||
ce.Write(zap.Error(err))
|
||||
}
|
||||
ok = false
|
||||
continue
|
||||
}
|
||||
lis := v2ray_simple.ListenSer(inServer, m.DefaultOutClient, &m.RoutingEnv, &m.GlobalInfo)
|
||||
if lis != nil {
|
||||
m.ListenCloserList = append(m.ListenCloserList, lis)
|
||||
m.AllServers = append(m.AllServers, inServer)
|
||||
|
||||
if hot {
|
||||
lis := v2ray_simple.ListenSer(inServer, m.DefaultOutClient, &m.RoutingEnv, &m.GlobalInfo)
|
||||
if lis != nil {
|
||||
m.ListenCloserList = append(m.ListenCloserList, lis)
|
||||
m.AllServers = append(m.AllServers, inServer)
|
||||
|
||||
} else {
|
||||
ok = false
|
||||
}
|
||||
} else {
|
||||
ok = false
|
||||
m.AllServers = append(m.AllServers, inServer)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// delete and stop the client
|
||||
func (m *M) HotDeleteClient(index int) {
|
||||
if index < 0 || index >= len(m.AllClients) {
|
||||
return
|
||||
@@ -88,6 +100,7 @@ func (m *M) HotDeleteClient(index int) {
|
||||
m.AllClients = utils.TrimSlice(m.AllClients, index)
|
||||
}
|
||||
|
||||
// delete and close the server
|
||||
func (m *M) HotDeleteServer(index int) {
|
||||
if index < 0 || index >= len(m.ListenCloserList) {
|
||||
return
|
||||
@@ -190,7 +203,7 @@ func (m *M) HotLoadDialUrl(theUrlStr string, format int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if !m.HotLoadDialConf("", []*proxy.DialConf{dc}) {
|
||||
if !m.LoadDialConf([]*proxy.DialConf{dc}) {
|
||||
return utils.ErrFailed
|
||||
}
|
||||
return nil
|
||||
@@ -219,7 +232,7 @@ func (m *M) HotLoadListenUrl(theUrlStr string, format int) error {
|
||||
fmt.Printf("parse url step 2 failed %v\n", err)
|
||||
return err
|
||||
}
|
||||
if !m.HotLoadListenConf([]*proxy.ListenConf{lc}) {
|
||||
if !m.LoadListenConf([]*proxy.ListenConf{lc}, true) {
|
||||
return utils.ErrFailed
|
||||
}
|
||||
return nil
|
||||
@@ -11,13 +11,16 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/e1732a364fed/v2ray_simple"
|
||||
"github.com/e1732a364fed/v2ray_simple/httpLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/proxy"
|
||||
)
|
||||
|
||||
type M struct {
|
||||
ApiServerConf
|
||||
DefaultUUID string
|
||||
|
||||
v2ray_simple.GlobalInfo
|
||||
|
||||
@@ -44,7 +47,21 @@ func New() *M {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *M) Cleanup() {
|
||||
func (m *M) Start() {
|
||||
if (m.DefaultOutClient != nil) && (len(m.AllServers) > 0) {
|
||||
for _, inServer := range m.AllServers {
|
||||
lis := v2ray_simple.ListenSer(inServer, m.DefaultOutClient, &m.RoutingEnv, &m.GlobalInfo)
|
||||
|
||||
if lis != nil {
|
||||
m.ListenCloserList = append(m.ListenCloserList, lis)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (m *M) Stop() {
|
||||
|
||||
for _, ser := range m.AllServers {
|
||||
if ser != nil {
|
||||
@@ -60,6 +77,32 @@ func (m *M) Cleanup() {
|
||||
|
||||
}
|
||||
|
||||
func (m *M) SetDefaultDirectClient() {
|
||||
m.AllClients = append(m.AllClients, v2ray_simple.DirectClient)
|
||||
m.DefaultOutClient = v2ray_simple.DirectClient
|
||||
|
||||
m.RoutingEnv.SetClient("direct", v2ray_simple.DirectClient)
|
||||
}
|
||||
|
||||
// 将fallback配置中的@转化成实际对应的server的地址
|
||||
func (m *M) ParseFallbacksAtSymbol(fs []*httpLayer.FallbackConf) {
|
||||
for _, fbConf := range fs {
|
||||
if fbConf.Dest == nil {
|
||||
continue
|
||||
}
|
||||
if deststr, ok := fbConf.Dest.(string); ok && strings.HasPrefix(deststr, "@") {
|
||||
for _, s := range m.AllServers {
|
||||
if s.GetTag() == deststr[1:] {
|
||||
//log.Println("got tag fallback dest, will set to ", s.AddrStr())
|
||||
fbConf.Dest = s.AddrStr()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (m *M) HasProxyRunning() bool {
|
||||
return len(m.ListenCloserList) > 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user