Skip to content

Commit 5e626f7

Browse files
authored
Merge pull request #406 from alireza0/consolidation
Consolidation
2 parents f1b6c8a + eb4c128 commit 5e626f7

Some content is hidden

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

103 files changed

+5737
-3503
lines changed

.github/workflows/docker-core.yml

Lines changed: 0 additions & 57 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
cd ..
6161
mv frontend/dist backend/web/html
6262
63-
- name: Build s-ui & singbox
63+
- name: Build s-ui
6464
run: |
6565
export CGO_ENABLED=1
6666
export GOOS=linux
@@ -88,27 +88,15 @@ jobs:
8888
export CC=s390x-linux-gnu-gcc
8989
fi
9090
91-
#### Build Sing-Box
92-
export VERSION=v1.10.1
93-
git clone -b $VERSION https://github.com/SagerNet/sing-box
94-
cd sing-box
95-
go build -tags with_quic,with_grpc,with_wireguard,with_ech,with_utls,with_reality_server,with_acme,with_v2ray_api,with_clash_api,with_gvisor \
96-
-v -trimpath -ldflags "-X 'github.com/sagernet/sing-box/constant.Version=${VERSION}' -s -w -buildid=" \
97-
-o sing-box ./cmd/sing-box
98-
cd ..
99-
10091
### Build s-ui
10192
cd backend
102-
go build -o ../sui main.go
93+
go build -ldflags="-w -s" -tags "with_quic,with_grpc,with_ech,with_utls,with_reality_server,with_acme,with_gvisor" -o ../sui main.go
10394
cd ..
10495
10596
mkdir s-ui
10697
cp sui s-ui/
10798
cp s-ui.service s-ui/
108-
cp sing-box.service s-ui/
109-
mkdir s-ui/bin
110-
cp sing-box/sing-box s-ui/bin/
111-
cp core/runSingbox.sh s-ui/bin/
99+
cp s-ui.sh s-ui/
112100
113101
- name: Package
114102
run: tar -zcvf s-ui-linux-${{ matrix.platform }}.tar.gz s-ui

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ENV GOARCH=$TARGETARCH
1212
RUN apk update && apk --no-cache --update add build-base gcc wget unzip
1313
COPY backend/ ./
1414
COPY --from=front-builder /app/dist/ /app/web/html/
15-
RUN go build -ldflags="-w -s" -o sui main.go
15+
RUN go build -ldflags="-w -s" -tags "with_quic,with_grpc,with_ech,with_utls,with_reality_server,with_acme,with_gvisor" -o sui main.go
1616

1717
FROM --platform=$TARGETPLATFORM alpine
1818
LABEL org.opencontainers.image.authors="alireza7@gmail.com"

backend/api/api.go

Lines changed: 102 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package api
22

33
import (
4+
"encoding/json"
45
"s-ui/logger"
56
"s-ui/service"
6-
"s-ui/singbox"
77
"s-ui/util"
8+
"s-ui/util/common"
89
"strconv"
910
"strings"
1011

@@ -17,11 +18,12 @@ type APIHandler struct {
1718
service.ConfigService
1819
service.ClientService
1920
service.TlsService
20-
service.InDataService
21+
service.InboundService
22+
service.OutboundService
23+
service.EndpointService
2124
service.PanelService
2225
service.StatsService
2326
service.ServerService
24-
singbox.Controller
2527
}
2628

2729
func NewAPIHandler(g *gin.RouterGroup) {
@@ -45,6 +47,8 @@ func (a *APIHandler) postHandler(c *gin.Context) {
4547
var err error
4648
action := c.Param("postAction")
4749
remoteIP := getRemoteIp(c)
50+
loginUser := GetLoginUser(c)
51+
hostname := getHostname(c)
4852

4953
switch action {
5054
case "login":
@@ -81,25 +85,31 @@ func (a *APIHandler) postHandler(c *gin.Context) {
8185
jsonMsg(c, "", err)
8286
}
8387
case "save":
84-
loginUser := GetLoginUser(c)
85-
data := map[string]string{}
86-
err = c.ShouldBind(&data)
87-
if err == nil {
88-
err = a.ConfigService.SaveChanges(data, loginUser)
88+
obj := c.Request.FormValue("object")
89+
act := c.Request.FormValue("action")
90+
data := c.Request.FormValue("data")
91+
objs, err := a.ConfigService.Save(obj, act, json.RawMessage(data), loginUser, hostname)
92+
if err != nil {
93+
jsonMsg(c, "save", err)
94+
return
8995
}
90-
jsonMsg(c, "save", err)
96+
err = a.loadPartialData(c, objs)
97+
if err != nil {
98+
jsonMsg(c, obj, err)
99+
}
100+
return
91101
case "restartApp":
92102
err = a.PanelService.RestartPanel(3)
93103
jsonMsg(c, "restartApp", err)
94104
case "restartSb":
95-
err = a.Controller.Restart()
105+
err = a.ConfigService.RestartCore()
96106
jsonMsg(c, "restartSb", err)
97107
case "linkConvert":
98108
link := c.Request.FormValue("link")
99109
result, _, err := util.GetOutbound(link, 0)
100110
jsonObj(c, result, err)
101111
default:
102-
jsonMsg(c, "API call", nil)
112+
jsonMsg(c, "failed", common.NewError("unknown action: ", action))
103113
}
104114
}
105115

@@ -121,6 +131,12 @@ func (a *APIHandler) getHandler(c *gin.Context) {
121131
return
122132
}
123133
jsonObj(c, data, nil)
134+
case "inbounds", "outbounds", "endpoints", "tls", "clients", "config":
135+
err := a.loadPartialData(c, []string{action})
136+
if err != nil {
137+
jsonMsg(c, action, err)
138+
}
139+
return
124140
case "users":
125141
users, err := a.UserService.GetUsers()
126142
if err != nil {
@@ -156,10 +172,9 @@ func (a *APIHandler) getHandler(c *gin.Context) {
156172
onlines, err := a.StatsService.GetOnlines()
157173
jsonObj(c, onlines, err)
158174
case "logs":
159-
service := c.Query("s")
160175
count := c.Query("c")
161176
level := c.Query("l")
162-
logs := a.ServerService.GetLogs(service, count, level)
177+
logs := a.ServerService.GetLogs(count, level)
163178
jsonObj(c, logs, nil)
164179
case "changes":
165180
actor := c.Query("a")
@@ -173,7 +188,7 @@ func (a *APIHandler) getHandler(c *gin.Context) {
173188
keypair := a.ServerService.GenKeypair(kType, options)
174189
jsonObj(c, keypair, nil)
175190
default:
176-
jsonMsg(c, "API call", nil)
191+
jsonMsg(c, "failed", common.NewError("unknown action: ", action))
177192
}
178193
}
179194

@@ -188,7 +203,7 @@ func (a *APIHandler) loadData(c *gin.Context) (interface{}, error) {
188203

189204
sysInfo := a.ServerService.GetSingboxInfo()
190205
if sysInfo["running"] == false {
191-
logs := a.ServerService.GetLogs("sing-box", "1", "debug")
206+
logs := a.ServerService.GetLogs("1", "debug")
192207
if len(logs) > 0 {
193208
data["lastLog"] = logs[0]
194209
}
@@ -198,7 +213,7 @@ func (a *APIHandler) loadData(c *gin.Context) (interface{}, error) {
198213
return "", err
199214
}
200215
if isUpdated {
201-
config, err := a.ConfigService.GetConfig()
216+
config, err := a.SettingService.GetConfig()
202217
if err != nil {
203218
return "", err
204219
}
@@ -210,18 +225,28 @@ func (a *APIHandler) loadData(c *gin.Context) (interface{}, error) {
210225
if err != nil {
211226
return "", err
212227
}
213-
inData, err := a.InDataService.GetAll()
228+
inbounds, err := a.InboundService.GetAll()
229+
if err != nil {
230+
return "", err
231+
}
232+
outbounds, err := a.OutboundService.GetAll()
233+
if err != nil {
234+
return "", err
235+
}
236+
endpoints, err := a.EndpointService.GetAll()
214237
if err != nil {
215238
return "", err
216239
}
217240
subURI, err := a.SettingService.GetFinalSubURI(strings.Split(c.Request.Host, ":")[0])
218241
if err != nil {
219242
return "", err
220243
}
221-
data["config"] = *config
244+
data["config"] = json.RawMessage(config)
222245
data["clients"] = clients
223246
data["tls"] = tlsConfigs
224-
data["inData"] = inData
247+
data["inbounds"] = inbounds
248+
data["outbounds"] = outbounds
249+
data["endpoints"] = endpoints
225250
data["subURI"] = subURI
226251
data["onlines"] = onlines
227252
} else {
@@ -230,3 +255,61 @@ func (a *APIHandler) loadData(c *gin.Context) (interface{}, error) {
230255

231256
return data, nil
232257
}
258+
259+
func (a *APIHandler) loadPartialData(c *gin.Context, objs []string) error {
260+
data := make(map[string]interface{}, 0)
261+
262+
for _, obj := range objs {
263+
switch obj {
264+
case "inbounds":
265+
id := c.Query("id")
266+
inbounds, err := a.InboundService.Get(id)
267+
if err != nil {
268+
return err
269+
}
270+
data[obj] = inbounds
271+
case "outbounds":
272+
outbounds, err := a.OutboundService.GetAll()
273+
if err != nil {
274+
return err
275+
}
276+
data[obj] = outbounds
277+
case "endpoints":
278+
endpoints, err := a.EndpointService.GetAll()
279+
if err != nil {
280+
return err
281+
}
282+
data[obj] = endpoints
283+
case "tls":
284+
tlsConfigs, err := a.TlsService.GetAll()
285+
if err != nil {
286+
return err
287+
}
288+
data[obj] = tlsConfigs
289+
case "clients":
290+
clients, err := a.ClientService.GetAll()
291+
if err != nil {
292+
return err
293+
}
294+
data[obj] = clients
295+
case "config":
296+
config, err := a.SettingService.GetConfig()
297+
if err != nil {
298+
return err
299+
}
300+
data[obj] = json.RawMessage(config)
301+
}
302+
}
303+
304+
jsonObj(c, data, nil)
305+
return nil
306+
}
307+
308+
func (a *APIHandler) postActions(c *gin.Context) (string, json.RawMessage, error) {
309+
var data map[string]json.RawMessage
310+
err := c.ShouldBind(&data)
311+
if err != nil {
312+
return "", nil, err
313+
}
314+
return string(data["action"]), data["data"], nil
315+
}

backend/api/utils.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ func getRemoteIp(c *gin.Context) string {
2727
}
2828
}
2929

30+
func getHostname(c *gin.Context) string {
31+
host := c.Request.Host
32+
if colonIndex := strings.LastIndex(host, ":"); colonIndex != -1 {
33+
host, _, _ = net.SplitHostPort(c.Request.Host)
34+
}
35+
return host
36+
}
37+
3038
func jsonMsg(c *gin.Context, msg string, err error) {
3139
jsonMsgObj(c, msg, nil, err)
3240
}
@@ -46,7 +54,7 @@ func jsonMsgObj(c *gin.Context, msg string, obj interface{}, err error) {
4654
}
4755
} else {
4856
m.Success = false
49-
m.Msg = msg + err.Error()
57+
m.Msg = msg + ": " + err.Error()
5058
logger.Warning("failed :", err)
5159
}
5260
c.JSON(http.StatusOK, m)

0 commit comments

Comments
 (0)