-
Notifications
You must be signed in to change notification settings - Fork 4
/
panel.go
133 lines (122 loc) · 3.88 KB
/
panel.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
package client3xui
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"strconv"
)
type PanelSettings struct {
WebListen string `json:"webListen"`
WebDomain string `json:"webDomain"`
WebPort int `json:"webPort"`
WebCertFile string `json:"webCertFile"`
WebKeyFile string `json:"webKeyFile"`
WebBasePath string `json:"webBasePath"`
SessionMaxAge int `json:"sessionMaxAge"`
PageSize int `json:"pageSize"`
ExpireDiff int `json:"expireDiff"`
TrafficDiff int `json:"trafficDiff"`
RemarkModel string `json:"remarkModel"`
TgBotEnable bool `json:"tgBotEnable"`
TgBotToken string `json:"tgBotToken"`
TgBotProxy string `json:"tgBotProxy"`
TgBotChatId string `json:"tgBotChatId"`
TgRunTime string `json:"tgRunTime"`
TgBotBackup bool `json:"tgBotBackup"`
TgBotLoginNotify bool `json:"tgBotLoginNotify"`
TgCpu int `json:"tgCpu"`
TgLang string `json:"tgLang"`
TimeLocation string `json:"timeLocation"`
SecretEnable bool `json:"secretEnable"`
SubEnable bool `json:"subEnable"`
SubListen string `json:"subListen"`
SubPort int `json:"subPort"`
SubPath string `json:"subPath"`
SubDomain string `json:"subDomain"`
SubCertFile string `json:"subCertFile"`
SubKeyFile string `json:"subKeyFile"`
SubUpdates int `json:"subUpdates"`
SubEncrypt bool `json:"subEncrypt"`
SubShowInfo bool `json:"subShowInfo"`
SubURI string `json:"subURI"`
SubJsonPath string `json:"subJsonPath"`
SubJsonURI string `json:"subJsonURI"`
SubJsonFragment string `json:"subJsonFragment"`
SubJsonMux string `json:"subJsonMux"`
SubJsonRules string `json:"subJsonRules"`
Datepicker string `json:"datepicker"`
}
type PanelSettingsResponse struct {
Success bool `json:"success"`
Msg string `json:"msg"`
Obj *PanelSettings `json:"obj"`
}
func (c *Client) GetPanelSettings(ctx context.Context) (*PanelSettingsResponse, error) {
resp := &PanelSettingsResponse{}
err := c.Do(ctx, http.MethodPost, "/panel/setting/all", nil, resp)
if err != nil {
return nil, err
}
if !resp.Success {
return resp, fmt.Errorf(resp.Msg)
}
return resp, err
}
func panelSettingsToMap(obj PanelSettings) map[string]string {
result := make(map[string]string)
v := reflect.ValueOf(obj)
t := reflect.TypeOf(obj)
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
key := t.Field(i).Tag.Get("json")
var value string
switch field.Kind() {
case reflect.String:
value = field.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value = strconv.FormatInt(field.Int(), 10)
case reflect.Bool:
value = strconv.FormatBool(field.Bool())
case reflect.Float32, reflect.Float64:
value = strconv.FormatFloat(field.Float(), 'f', -1, 64)
default:
value = fmt.Sprintf("%v", field.Interface())
}
result[key] = value
}
return result
}
func (c *Client) EditPanelSettings(ctx context.Context, settings PanelSettings) error {
settingsMap := panelSettingsToMap(settings)
formValues := url.Values{}
for k, v := range settingsMap {
formValues.Add(k, v)
}
resp, err := c.DoRaw(ctx, http.MethodPost, c.url, "/panel/setting/update",
"application/x-www-form-urlencoded", []byte(formValues.Encode()))
if err != nil {
return err
}
var genericResp ApiResponse
if err := json.Unmarshal(resp, &genericResp); err != nil {
return err
}
if !genericResp.Success {
return fmt.Errorf(genericResp.Msg)
}
return nil
}
func (c *Client) RestartPanel(ctx context.Context) (*ApiResponse, error) {
resp := &ApiResponse{}
err := c.Do(ctx, http.MethodPost, "/panel/setting/restartPanel", nil, resp)
if err != nil {
return nil, err
}
if !resp.Success {
return resp, fmt.Errorf(resp.Msg)
}
return resp, err
}