2024-12-03 21:47:12 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
type Action struct {
|
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
Method Method `json:"method"`
|
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
|
DefaultValue interface{} `json:"default-value"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ActionResult struct {
|
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
Method Method `json:"method"`
|
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (result ActionResult) Json() ([]byte, error) {
|
|
|
|
|
data, err := json.Marshal(result)
|
2024-12-03 21:47:12 +08:00
|
|
|
return data, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
func (action Action) getResult(data interface{}) []byte {
|
|
|
|
|
resultAction := ActionResult{
|
2024-12-03 21:47:12 +08:00
|
|
|
Id: action.Id,
|
|
|
|
|
Method: action.Method,
|
|
|
|
|
Data: data,
|
|
|
|
|
}
|
2025-02-09 18:39:38 +08:00
|
|
|
res, _ := resultAction.Json()
|
2025-01-13 19:08:17 +08:00
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
func handleAction(action *Action, result func(data interface{})) {
|
2025-01-13 19:08:17 +08:00
|
|
|
switch action.Method {
|
|
|
|
|
case initClashMethod:
|
2025-04-09 16:46:14 +08:00
|
|
|
paramsString := action.Data.(string)
|
|
|
|
|
result(handleInitClash(paramsString))
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case getIsInitMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleGetIsInit())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case forceGcMethod:
|
|
|
|
|
handleForceGc()
|
2025-02-09 18:39:38 +08:00
|
|
|
result(true)
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case shutdownMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleShutdown())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case validateConfigMethod:
|
|
|
|
|
data := []byte(action.Data.(string))
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleValidateConfig(data))
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case updateConfigMethod:
|
|
|
|
|
data := []byte(action.Data.(string))
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleUpdateConfig(data))
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case getProxiesMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleGetProxies())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case changeProxyMethod:
|
|
|
|
|
data := action.Data.(string)
|
|
|
|
|
handleChangeProxy(data, func(value string) {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(value)
|
2025-01-13 19:08:17 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
case getTrafficMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleGetTraffic())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case getTotalTrafficMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleGetTotalTraffic())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case resetTrafficMethod:
|
|
|
|
|
handleResetTraffic()
|
2025-02-09 18:39:38 +08:00
|
|
|
result(true)
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case asyncTestDelayMethod:
|
|
|
|
|
data := action.Data.(string)
|
|
|
|
|
handleAsyncTestDelay(data, func(value string) {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(value)
|
2025-01-13 19:08:17 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
case getConnectionsMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleGetConnections())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case closeConnectionsMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleCloseConnections())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case closeConnectionMethod:
|
|
|
|
|
id := action.Data.(string)
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleCloseConnection(id))
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case getExternalProvidersMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleGetExternalProviders())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case getExternalProviderMethod:
|
|
|
|
|
externalProviderName := action.Data.(string)
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleGetExternalProvider(externalProviderName))
|
2025-01-13 19:08:17 +08:00
|
|
|
case updateGeoDataMethod:
|
|
|
|
|
paramsString := action.Data.(string)
|
|
|
|
|
var params = map[string]string{}
|
|
|
|
|
err := json.Unmarshal([]byte(paramsString), ¶ms)
|
|
|
|
|
if err != nil {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(err.Error())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
geoType := params["geo-type"]
|
|
|
|
|
geoName := params["geo-name"]
|
|
|
|
|
handleUpdateGeoData(geoType, geoName, func(value string) {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(value)
|
2025-01-13 19:08:17 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
case updateExternalProviderMethod:
|
|
|
|
|
providerName := action.Data.(string)
|
|
|
|
|
handleUpdateExternalProvider(providerName, func(value string) {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(value)
|
2025-01-13 19:08:17 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
case sideLoadExternalProviderMethod:
|
|
|
|
|
paramsString := action.Data.(string)
|
|
|
|
|
var params = map[string]string{}
|
|
|
|
|
err := json.Unmarshal([]byte(paramsString), ¶ms)
|
|
|
|
|
if err != nil {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(err.Error())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
providerName := params["providerName"]
|
|
|
|
|
data := params["data"]
|
|
|
|
|
handleSideLoadExternalProvider(providerName, []byte(data), func(value string) {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(value)
|
2025-01-13 19:08:17 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
case startLogMethod:
|
|
|
|
|
handleStartLog()
|
2025-02-09 18:39:38 +08:00
|
|
|
result(true)
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case stopLogMethod:
|
|
|
|
|
handleStopLog()
|
2025-02-09 18:39:38 +08:00
|
|
|
result(true)
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case startListenerMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleStartListener())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case stopListenerMethod:
|
2025-02-09 18:39:38 +08:00
|
|
|
result(handleStopListener())
|
2025-01-13 19:08:17 +08:00
|
|
|
return
|
|
|
|
|
case getCountryCodeMethod:
|
|
|
|
|
ip := action.Data.(string)
|
|
|
|
|
handleGetCountryCode(ip, func(value string) {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(value)
|
2025-01-13 19:08:17 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
case getMemoryMethod:
|
|
|
|
|
handleGetMemory(func(value string) {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(value)
|
2025-01-13 19:08:17 +08:00
|
|
|
})
|
|
|
|
|
return
|
2025-02-09 18:39:38 +08:00
|
|
|
case getProfileMethod:
|
|
|
|
|
profileId := action.Data.(string)
|
|
|
|
|
handleGetMemory(func(value string) {
|
|
|
|
|
result(handleGetProfile(profileId))
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
case setStateMethod:
|
|
|
|
|
data := action.Data.(string)
|
|
|
|
|
handleSetState(data)
|
|
|
|
|
result(true)
|
2025-04-18 17:50:46 +08:00
|
|
|
case crashMethod:
|
|
|
|
|
result(true)
|
|
|
|
|
handleCrash()
|
2025-01-13 19:08:17 +08:00
|
|
|
default:
|
2025-02-09 18:39:38 +08:00
|
|
|
handle := nextHandle(action, result)
|
2025-01-13 19:08:17 +08:00
|
|
|
if handle {
|
|
|
|
|
return
|
|
|
|
|
} else {
|
2025-02-09 18:39:38 +08:00
|
|
|
result(action.DefaultValue)
|
2025-01-13 19:08:17 +08:00
|
|
|
}
|
2024-12-03 21:47:12 +08:00
|
|
|
}
|
|
|
|
|
}
|