Skip to content

Commit a48497a

Browse files
author
sipt
authored
Merge pull request #63 from sipt/feature/api
Feature/api
2 parents c9c9448 + 1066df7 commit a48497a

File tree

759 files changed

+23267
-235
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

759 files changed

+23267
-235
lines changed

assets/assets_release.go

Lines changed: 20837 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ else
1111
rm -rf _output/shuttle/*
1212
fi
1313

14-
cp -rf view _output/shuttle/
15-
cp GeoLite2-Country.mmdb _output/shuttle/
1614
#if [ -d ".conf/" ];then
1715
#cp .conf/sipt.yaml _output/shuttle.yaml
1816
#else
19-
cp example.yaml _output/shuttle/shuttle.yaml
17+
cp example.yaml _output/shuttle/example.yaml
2018
#fi
21-
mkdir _output/shuttle/RespFiles
19+
cd assets
20+
go generate
21+
cd ..
2222

2323
if [ "$1" == "mac" ];then
2424
# mac os
25-
GOOS=darwin GOARCH=amd64 go build -o _output/shuttle/shuttle cmd/main.go
25+
GOOS=darwin GOARCH=amd64 go build -tags release -o _output/shuttle/shuttle ./cmd/
2626
GOOS=darwin GOARCH=amd64 go build -o _output/shuttle/upgrade scripts/upgrade.go
2727
`echo "#!/usr/bin/env bash
2828
nohup ./shuttle >> shuttle.log 2>&1 &" > _output/shuttle/start.sh`
2929
`chmod a+x _output/shuttle/start.sh`
3030
elif [ "$1" == "win" ];then
3131
# windows
32-
GOOS=windows GOARCH=amd64 go build -o _output/shuttle/shuttle.exe cmd/main.go
32+
GOOS=windows GOARCH=amd64 go build -tags release -o _output/shuttle/shuttle.exe ./cmd/
3333
GOOS=windows GOARCH=amd64 go build -o _output/shuttle/upgrade.exe scripts/upgrade.go
3434
`echo "@echo off
3535
if \"%1\" == \"h\" goto begin
@@ -38,7 +38,7 @@ mshta vbscript:createobject(\"wscript.shell\").run(\"%~nx0 h\",0)(window.close)&
3838
shuttle >> shuttle.log" > _output/shuttle/startup.bat`
3939
elif [ "$1" == "linux" ];then
4040
# linux
41-
GOOS=linux GOARCH=amd64 go build -o _output/shuttle/shuttle cmd/main.go
41+
GOOS=linux GOARCH=amd64 go build -tags release -o _output/shuttle/shuttle ./cmd/
4242
GOOS=linux GOARCH=amd64 go build -o _output/shuttle/upgrade scripts/upgrade.go
4343
`echo "#!/usr/bin/env bash
4444
nohup ./shuttle >> shuttle.log 2>&1 &" > _output/shuttle/start.sh`

ciphers/ssaead/cipher_conn.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ var aeadCiphers = make(map[string]IAEADCipher)
1818

1919
func registerAEADCiphers(method string, c IAEADCipher) {
2020
aeadCiphers[method] = c
21-
log.Logger.Infof("[SS Ciphers] register cipher [%s]", method)
2221
}
2322

2423
func GetAEADCiphers(method string) func(string, connect.IConn) (connect.IConn, error) {

ciphers/ssstream/cipher_conn.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ var streamCiphers = make(map[string]IStreamCipher)
1414

1515
func registerStreamCiphers(method string, c IStreamCipher) {
1616
streamCiphers[method] = c
17-
log.Logger.Infof("[SS Ciphers] register cipher [%s]", method)
1817
}
1918

2019
func GetStreamCiphers(method string) func(string, connect.IConn) (connect.IConn, error) {

cmd/event_center.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/sipt/shuttle/config"
6+
. "github.com/sipt/shuttle/constant"
7+
"github.com/sipt/shuttle/controller"
8+
"github.com/sipt/shuttle/log"
9+
"os"
10+
"os/exec"
11+
"runtime"
12+
)
13+
14+
var eventChan chan *EventObj
15+
16+
func ListenEvent() {
17+
eventChan = make(chan *EventObj, 1)
18+
go dealEvent(eventChan)
19+
}
20+
21+
func dealEvent(c chan *EventObj) {
22+
for {
23+
t := <-c
24+
switch t.Type {
25+
case EventShutdown.Type:
26+
log.Logger.Info("[Shuttle] is shutdown, see you later!")
27+
shutdown(config.CurrentConfig().General.SetAsSystemProxy)
28+
os.Exit(0)
29+
return
30+
case EventReloadConfig.Type:
31+
_, err := reloadConfig(config.CurrentConfigFile(), StopSocksSignal, StopHTTPSignal)
32+
if err != nil {
33+
log.Logger.Error("Reload Config failed: ", err)
34+
fmt.Println(err.Error())
35+
os.Exit(1)
36+
}
37+
case EventRestartHttpProxy.Type:
38+
StopHTTPSignal <- true
39+
go HandleHTTP(config.CurrentConfig(), StopHTTPSignal)
40+
case EventRestartSocksProxy.Type:
41+
StopSocksSignal <- true
42+
go HandleSocks5(config.CurrentConfig(), StopSocksSignal)
43+
case EventRestartController.Type:
44+
controller.ShutdownController()
45+
go controller.StartController(config.CurrentConfig(), eventChan)
46+
case EventUpgrade.Type:
47+
//todo
48+
fileName := t.GetData().(string)
49+
shutdown(config.CurrentConfig().General.SetAsSystemProxy)
50+
log.Logger.Info("[Shuttle] is shutdown, for upgrade!")
51+
var name string
52+
if runtime.GOOS == "windows" {
53+
name = "upgrade"
54+
} else {
55+
name = "./upgrade"
56+
}
57+
cmd := exec.Command(name, "-f="+fileName)
58+
err := cmd.Start()
59+
if err != nil {
60+
fmt.Println(err.Error())
61+
}
62+
os.Exit(0)
63+
}
64+
}
65+
}

cmd/main.go

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"io/ioutil"
76
"net"
87
"os"
9-
"os/exec"
108
"os/signal"
11-
"path/filepath"
12-
"runtime"
139
"runtime/debug"
1410
"strings"
1511
"syscall"
@@ -30,11 +26,8 @@ import (
3026
)
3127

3228
var (
33-
ShutdownSignal = make(chan bool, 1)
34-
UpgradeSignal = make(chan string, 1)
35-
StopSocksSignal = make(chan bool, 1)
36-
StopHTTPSignal = make(chan bool, 1)
37-
ReloadConfigSignal = make(chan bool, 1)
29+
StopSocksSignal = make(chan bool, 1)
30+
StopHTTPSignal = make(chan bool, 1)
3831
)
3932

4033
func main() {
@@ -56,15 +49,14 @@ func main() {
5649
return
5750
}
5851

52+
//event listen
53+
ListenEvent()
54+
5955
// 启动api控制
60-
go controller.StartController(conf,
61-
ShutdownSignal, // shutdown program
62-
ReloadConfigSignal, // reload config
63-
UpgradeSignal, // upgrade
64-
)
56+
go controller.StartController(conf, eventChan)
6557
//go HandleUDP()
66-
go HandleHTTP(conf, StopSocksSignal)
67-
go HandleSocks5(conf, StopHTTPSignal)
58+
go HandleHTTP(conf, StopHTTPSignal)
59+
go HandleSocks5(conf, StopSocksSignal)
6860

6961
// Catch "Ctrl + C"
7062
signalChan := make(chan os.Signal, 1)
@@ -74,43 +66,12 @@ func main() {
7466
EnableSystemProxy(conf)
7567
}
7668
fmt.Println("success")
77-
for {
78-
select {
79-
case fileName := <-UpgradeSignal:
80-
shutdown(conf.General.SetAsSystemProxy)
81-
log.Logger.Info("[Shuttle] is shutdown, for upgrade!")
82-
var name string
83-
if runtime.GOOS == "windows" {
84-
name = "upgrade"
85-
} else {
86-
name = "./upgrade"
87-
}
88-
cmd := exec.Command(name, "-f="+fileName)
89-
err = cmd.Start()
90-
if err != nil {
91-
ioutil.WriteFile(filepath.Join(*logPath, "logs", "error.log"), []byte(err.Error()), 0664)
92-
}
93-
ioutil.WriteFile(filepath.Join(*logPath, "logs", "end.log"), []byte("ending"), 0664)
94-
os.Exit(0)
95-
case <-ShutdownSignal:
96-
log.Logger.Info("[Shuttle] is shutdown, see you later!")
97-
shutdown(conf.General.SetAsSystemProxy)
98-
os.Exit(0)
99-
return
100-
case <-signalChan:
101-
log.Logger.Info("[Shuttle] is shutdown, see you later!")
102-
shutdown(conf.General.SetAsSystemProxy)
103-
os.Exit(0)
104-
return
105-
case <-ReloadConfigSignal:
106-
conf, err = reloadConfig(*configPath, StopSocksSignal, StopHTTPSignal)
107-
if err != nil {
108-
log.Logger.Error("Reload Config failed: ", err)
109-
fmt.Println(err.Error())
110-
os.Exit(1)
111-
}
112-
}
113-
}
69+
70+
<-signalChan
71+
log.Logger.Info("[Shuttle] is shutdown, see you later!")
72+
shutdown(conf.General.SetAsSystemProxy)
73+
os.Exit(0)
74+
return
11475
}
11576

11677
//load config
@@ -160,16 +121,9 @@ func reloadConfig(configPath string, StopSocksSignal, StopHTTPSignal chan bool)
160121
if oldConf.GetControllerInterface() != conf.GetControllerInterface() ||
161122
oldConf.GetControllerPort() != conf.GetControllerPort() {
162123
//restart controller
163-
err = controller.ShutdownController()
164-
if err != nil {
165-
return
166-
}
124+
controller.ShutdownController()
167125
// 启动api控制
168-
go controller.StartController(conf,
169-
ShutdownSignal, // shutdown program
170-
ReloadConfigSignal, // reload config
171-
UpgradeSignal, // upgrade
172-
)
126+
go controller.StartController(conf, eventChan)
173127
}
174128

175129
// http proxy

config/config.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package config
22

33
import (
44
"fmt"
5+
"github.com/sipt/shuttle/util"
56
"github.com/sipt/yaml"
67
"io/ioutil"
78
)
89

9-
const ShuttleVersion = "v5.1.0"
1010
const ConfigFileVersion = "v1.0.1"
1111
const SetAsSystemProxyAuto = "auto"
1212

@@ -23,6 +23,8 @@ func CurrentConfigFile() string {
2323

2424
// load config file
2525
func LoadConfig(filePath string) (*Config, error) {
26+
util.RLock(filePath)
27+
defer util.RUnLock(filePath)
2628
data, err := ioutil.ReadFile(filePath)
2729
if err != nil {
2830
return nil, fmt.Errorf("read config file failed: %v", err)
@@ -42,6 +44,8 @@ func LoadConfig(filePath string) (*Config, error) {
4244

4345
// save config file
4446
func SaveConfig(configFile string, config *Config) error {
47+
util.Lock(configFile)
48+
defer util.UnLock(configFile)
4549
bytes, err := yaml.Marshal(config)
4650
if err != nil {
4751
return fmt.Errorf("[CONF] yaml marshal config failed : %v", err)
@@ -76,6 +80,7 @@ type Config struct {
7680
Mitm *Mitm `yaml:"MITM"`
7781
Rule [][]string `yaml:"Rule,[flow],2quoted"`
7882
HttpMap *HttpMap `yaml:"Http-Map"`
83+
RttUrl string `yaml:"rtt-url"`
7984
}
8085

8186
type General struct {
@@ -97,14 +102,14 @@ type Mitm struct {
97102
}
98103

99104
type HttpMap struct {
100-
ReqMap []*ModifyMap `yaml:"Req-Map,2quoted"`
101-
RespMap []*ModifyMap `yaml:"Resp-Map,2quoted"`
105+
ReqMap []*ModifyMap `yaml:"Req-Map,2quoted" json:"req_map"`
106+
RespMap []*ModifyMap `yaml:"Resp-Map,2quoted" json:"resp_map"`
102107
}
103108

104109
type ModifyMap struct {
105-
Type string `yaml:"type,2quoted"`
106-
UrlRex string `yaml:"url-rex,2quoted"`
107-
Items [][]string `yaml:"items,[flow],2quoted"`
110+
Type string `yaml:"type,2quoted" json:"type"`
111+
UrlRex string `yaml:"url-rex,2quoted" json:"url_rex"`
112+
Items [][]string `yaml:"items,[flow],2quoted" json:"items"`
108113
}
109114

110115
//dns
@@ -130,6 +135,9 @@ func (c *Config) GetGeoIPDBFile() string {
130135
func (c *Config) GetLogLevel() string {
131136
return c.General.LogLevel
132137
}
138+
func (c *Config) SetLogLevel(l string) {
139+
c.General.LogLevel = l
140+
}
133141

134142
//controller
135143
func (c *Config) GetControllerDomain() string {
@@ -189,6 +197,12 @@ func (c *Config) GetProxyGroup() map[string][]string {
189197
func (c *Config) SetProxyGroup(group map[string][]string) {
190198
c.ProxyGroup = group
191199
}
200+
func (c *Config) SetRttUrl(rttUrl string) {
201+
c.RttUrl = rttUrl
202+
}
203+
func (c *Config) GetRttUrl() string {
204+
return c.RttUrl
205+
}
192206

193207
//Rule
194208
func (c *Config) GetRule() [][]string {

config/version.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package config
2+
3+
const ShuttleVersion = "v6.0.0"

constant/constant.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package constant
2+
3+
var (
4+
EventShutdown = &EventObj{Type: 0}
5+
EventReloadConfig = &EventObj{Type: 1}
6+
EventRestartHttpProxy = &EventObj{Type: 2}
7+
EventRestartSocksProxy = &EventObj{Type: 3}
8+
EventRestartController = &EventObj{Type: 4}
9+
EventUpgrade = &EventObj{Type: 5}
10+
)
11+
12+
type EventObj struct {
13+
Type int
14+
data interface{}
15+
}
16+
17+
func (e *EventObj) SetData(data interface{}) *EventObj {
18+
eb := &EventObj{}
19+
*eb = *e
20+
eb.data = data
21+
return eb
22+
}
23+
24+
func (e *EventObj) GetData() interface{} {
25+
return e.data
26+
}

controller/api/api_router.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package api
22

33
import (
44
"github.com/gin-gonic/gin"
5+
. "github.com/sipt/shuttle/constant"
56
)
67

7-
func APIRoute(router *gin.RouterGroup, shutdownSingnal chan bool, reloadConfigSignal chan bool, upgradeSignal chan string) {
8+
func APIRoute(router *gin.RouterGroup, eventChan chan *EventObj) {
89
//dns
910
router.GET("/dns", DNSCacheList)
1011
router.DELETE("/dns", ClearDNSCache)
@@ -26,11 +27,6 @@ func APIRoute(router *gin.RouterGroup, shutdownSingnal chan bool, reloadConfigSi
2627
router.POST("/cert", GenerateCert)
2728
router.GET("/cert", DownloadCert)
2829

29-
//MitM rules
30-
router.GET("/mitm/rules", GetMitMRules)
31-
router.POST("/mitm/rules", AppendMitMRules)
32-
router.DELETE("/mitm/rules", DelMitMRules)
33-
3430
//server
3531
router.GET("/servers", ServerList)
3632
router.POST("/server/select", SelectServer)
@@ -39,12 +35,12 @@ func APIRoute(router *gin.RouterGroup, shutdownSingnal chan bool, reloadConfigSi
3935
//general
4036
router.GET("/system/proxy/enable", EnableSystemProxy)
4137
router.GET("/system/proxy/disable", DisableSystemProxy)
42-
router.POST("/shutdown", NewShutdown(shutdownSingnal))
43-
router.POST("/reload", ReloadConfig(reloadConfigSignal))
38+
router.POST("/shutdown", NewShutdown(eventChan))
39+
router.POST("/reload", ReloadConfig(eventChan))
4440
router.GET("/mode", GetConnMode)
4541
router.POST("/mode/:mode", SetConnMode)
4642
router.GET("/upgrade/check", CheckUpdate)
47-
router.POST("/upgrade", NewUpgrade(upgradeSignal))
43+
router.POST("/upgrade", NewUpgrade(eventChan))
4844

4945
//ws
5046
router.GET("/ws/records", func(ctx *gin.Context) {

0 commit comments

Comments
 (0)