diff --git a/web/assets/js/model/outbound.js b/web/assets/js/model/outbound.js index b8ce2f77..0191c4a4 100644 --- a/web/assets/js/model/outbound.js +++ b/web/assets/js/model/outbound.js @@ -988,7 +988,7 @@ Outbound.WireguardSettings.Peer = class extends CommonClass { return { publicKey: this.publicKey, preSharedKey: this.psk.length>0 ? this.psk : undefined, - allowedIPs: this.allowedIPs ? this.allowedIPs.split(",") : undefined, + allowedIPs: this.allowedIPs ? this.allowedIPs : undefined, endpoint: this.endpoint, keepAlive: this.keepAlive?? undefined, }; diff --git a/web/controller/xray_setting.go b/web/controller/xray_setting.go index d4deacd9..09e9115f 100644 --- a/web/controller/xray_setting.go +++ b/web/controller/xray_setting.go @@ -26,6 +26,7 @@ func (a *XraySettingController) initRouter(g *gin.RouterGroup) { g.POST("/update", a.updateSetting) g.GET("/getXrayResult", a.getXrayResult) g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig) + g.POST("/warp/:action", a.warp) } func (a *XraySettingController) getXraySetting(c *gin.Context) { @@ -61,3 +62,25 @@ func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) { func (a *XraySettingController) getXrayResult(c *gin.Context) { jsonObj(c, a.XrayService.GetXrayResult(), nil) } + +func (a *XraySettingController) warp(c *gin.Context) { + action := c.Param("action") + var resp string + var err error + switch action { + case "data": + resp, err = a.XraySettingService.GetWarp() + case "config": + resp, err = a.XraySettingService.GetWarpConfig() + case "reg": + skey := c.PostForm("privateKey") + pkey := c.PostForm("publicKey") + resp, err = a.XraySettingService.RegWarp(skey, pkey) + case "license": + license := c.PostForm("license") + println(license) + resp, err = a.XraySettingService.SetWarpLicence(license) + } + + jsonObj(c, resp, err) +} diff --git a/web/html/xui/warp_modal.html b/web/html/xui/warp_modal.html new file mode 100644 index 00000000..c9f86603 --- /dev/null +++ b/web/html/xui/warp_modal.html @@ -0,0 +1,204 @@ +{{define "warpModal"}} + + + + + +{{end}} diff --git a/web/html/xui/xray.html b/web/html/xui/xray.html index 0cf7fd05..24035316 100644 --- a/web/html/xui/xray.html +++ b/web/html/xui/xray.html @@ -6,6 +6,7 @@ + @@ -80,12 +81,16 @@ - - - + @@ -217,10 +222,12 @@ + @@ -334,6 +341,7 @@ {{ i18n "pages.xray.outbound.addOutbound" }} + WARP const rulesColumns = [ { title: "#", align: 'center', width: 15, scopedSlots: { customRender: 'action' } }, { title: '{{ i18n "pages.xray.rules.source"}}', children: [ { title: 'IP', dataIndex: "source", align: 'center', width: 20, ellipsis: true }, - { title: 'port', dataIndex: 'sourcePort', align: 'center', width: 10, ellipsis: true } ]}, + { title: 'Port', dataIndex: 'sourcePort', align: 'center', width: 10, ellipsis: true } ]}, { title: '{{ i18n "pages.inbounds.network"}}', children: [ { title: 'L4', dataIndex: 'network', align: 'center', width: 10 }, { title: 'Protocol', dataIndex: 'protocol', align: 'center', width: 10, ellipsis: true }, @@ -461,6 +470,7 @@ { title: '{{ i18n "pages.xray.outbound.tag"}}', dataIndex: 'tag', align: 'center', width: 50 }, { title: '{{ i18n "pages.xray.outbound.domain"}}', dataIndex: 'domain', align: 'center', width: 50 }, ]; + const app = new Vue({ delimiters: ['[[', ']]'], el: '#app', @@ -505,23 +515,10 @@ domainStrategy: "UseIPv4" } }, - warpSettings: { - tag: "WARP", - protocol: "socks", - settings: { - servers: [ - { - address: "127.0.0.1", - port: 40000 - } - ] - } - }, directSettings: { tag: "direct", protocol: "freedom" }, - OutboundDomainStrategies: ["AsIs", "UseIP", "UseIPv4", "UseIPv6"], routingDomainStrategies: ["AsIs", "IPIfNonMatch", "IPOnDemand"], settingsData: { protocols: { @@ -532,7 +529,7 @@ cn: ["geoip:cn"], ir: ["ext:geoip_IR.dat:ir"], ru: ["geoip:ru"], - vn: ["ext:geoip_VN.dat:vn"], + vn: ["ext:geoip_VN.dat:vn"], }, domains: { ads: [ @@ -562,11 +559,11 @@ "regexp:.*\\.xn--mgba3a4f16a$", // .ایران "ext:geosite_IR.dat:ir" ], - vn: [ - "regexp:.*\\.vn$", + vn: [ + "regexp:.*\\.vn$", "ext:geosite_VN.dat:vn", "ext:geosite_VN.dat:ads" - ] + ] }, familyProtectDNS: { "servers": [ @@ -670,13 +667,13 @@ } }, syncRulesWithOutbound(tag, setting) { - const newTemplateSettings = {...this.templateSettings}; + const newTemplateSettings = this.templateSettings; const haveRules = newTemplateSettings.routing.rules.some((r) => r?.outboundTag === tag); const outboundIndex = newTemplateSettings.outbounds.findIndex((o) => o.tag === tag); - if (!haveRules && outboundIndex >= 0) { - newTemplateSettings.outbounds.splice(outboundIndex, 1); + if (!haveRules && outboundIndex > 0) { + newTemplateSettings.outbounds.splice(outboundIndex); } - if (haveRules && outboundIndex === -1) { + if (haveRules && outboundIndex < 0) { newTemplateSettings.outbounds.push(setting); } this.templateSettings = newTemplateSettings; @@ -774,6 +771,8 @@ break; case Protocols.DNS: return [o.settings.address + ':' + o.settings.port]; + case Protocols.Wireguard: + return o.settings.peers.map(peer => peer.endpoint); default: return null; } @@ -939,13 +938,16 @@ rules = this.templateSettings.routing.rules; rules.splice(index,1); this.routingRuleSettings = JSON.stringify(rules); + }, + showWarp(){ + warpModal.show(); } }, async mounted() { await this.getXraySetting(); await this.getXrayResult(); while (true) { - await PromiseUtil.sleep(600); + await PromiseUtil.sleep(800); this.saveBtnDisable = this.oldXraySetting === this.xraySetting; } }, @@ -1113,11 +1115,10 @@ }, warpDomains: { get: function () { - return this.templateRuleGetter({ outboundTag: "WARP", property: "domain" }); + return this.templateRuleGetter({ outboundTag: "warp", property: "domain" }); }, set: function (newValue) { - this.templateRuleSetter({ outboundTag: "WARP", property: "domain", data: newValue }); - this.syncRulesWithOutbound("WARP", this.warpSettings); + this.templateRuleSetter({ outboundTag: "warp", property: "domain", data: newValue }); } }, manualBlockedIPs: { @@ -1435,6 +1436,11 @@ } } }, + WarpExist: { + get: function() { + return this.templateSettings ? this.templateSettings.outbounds.findIndex((o) => o.tag == "warp")>=0 : false; + }, + }, GoogleWARPSettings: { get: function () { return doAllItemsExist(this.settingsData.domains.google, this.warpDomains); diff --git a/web/service/setting.go b/web/service/setting.go index 25770373..89c78662 100644 --- a/web/service/setting.go +++ b/web/service/setting.go @@ -58,6 +58,7 @@ var defaultValueMap = map[string]string{ "subShowInfo": "true", "subURI": "", "datepicker": "gregorian", + "warp": "", } type SettingService struct { @@ -423,6 +424,10 @@ func (s *SettingService) GetSubShowInfo() (bool, error) { return s.getBool("subShowInfo") } +func (s *SettingService) GetPageSize() (int, error) { + return s.getInt("pageSize") +} + func (s *SettingService) GetSubURI() (string, error) { return s.getString("subURI") } @@ -431,8 +436,11 @@ func (s *SettingService) GetDatepicker() (string, error) { return s.getString("datepicker") } -func (s *SettingService) GetPageSize() (int, error) { - return s.getInt("pageSize") +func (s *SettingService) GetWarp() (string, error) { + return s.getString("warp") +} +func (s *SettingService) SetWarp(data string) error { + return s.setString("warp", data) } func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error { diff --git a/web/service/xray_setting.go b/web/service/xray_setting.go index 4550bde2..1d2e696e 100644 --- a/web/service/xray_setting.go +++ b/web/service/xray_setting.go @@ -1,8 +1,13 @@ package service import ( + "bytes" _ "embed" "encoding/json" + "fmt" + "net/http" + "os" + "time" "x-ui/util/common" "x-ui/xray" ) @@ -26,3 +31,142 @@ func (s *XraySettingService) CheckXrayConfig(XrayTemplateConfig string) error { } return nil } + +func (s *XraySettingService) GetWarpData() (string, error) { + warp, err := s.SettingService.GetWarp() + if err != nil { + return "", err + } + return warp, nil +} + +func (s *XraySettingService) GetWarpConfig() (string, error) { + var warpData map[string]string + warp, err := s.SettingService.GetWarp() + if err != nil { + return "", err + } + err = json.Unmarshal([]byte(warp), &warpData) + if err != nil { + return "", err + } + + url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s", warpData["device_id"]) + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return "", err + } + req.Header.Set("Authorization", "Bearer "+warpData["access_token"]) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + buffer := bytes.NewBuffer(make([]byte, 8192)) + buffer.Reset() + _, err = buffer.ReadFrom(resp.Body) + if err != nil { + return "", err + } + + return buffer.String(), nil +} + +func (s *XraySettingService) RegWarp(secretKey string, publicKey string) (string, error) { + tos := time.Now().UTC().Format("2006-01-02T15:04:05.000Z") + hostName, _ := os.Hostname() + data := fmt.Sprintf(`{"key":"%s","tos":"%s","type": "PC","model": "x-ui", "name": "%s"}`, publicKey, tos, hostName) + + url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg") + + req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data))) + if err != nil { + return "", err + } + + req.Header.Add("CF-Client-Version", "a-7.21-0721") + req.Header.Add("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + buffer := bytes.NewBuffer(make([]byte, 8192)) + buffer.Reset() + _, err = buffer.ReadFrom(resp.Body) + if err != nil { + return "", err + } + + var rspData map[string]interface{} + err = json.Unmarshal(buffer.Bytes(), &rspData) + if err != nil { + return "", err + } + + deviceId := rspData["id"].(string) + token := rspData["token"].(string) + license, ok := rspData["account"].(map[string]interface{})["license"].(string) + if !ok { + fmt.Println("Error accessing license value.") + return "", err + } + + warpData := fmt.Sprintf("{\n \"access_token\": \"%s\",\n \"device_id\": \"%s\",", token, deviceId) + warpData += fmt.Sprintf("\n \"license_key\": \"%s\",\n \"private_key\": \"%s\"\n}", license, secretKey) + + s.SettingService.SetWarp(warpData) + + result := fmt.Sprintf("{\n \"data\": %s,\n \"config\": %s\n}", warpData, buffer.String()) + + return result, nil +} + +func (s *XraySettingService) SetWarpLicence(license string) (string, error) { + var warpData map[string]string + warp, err := s.SettingService.GetWarp() + if err != nil { + return "", err + } + err = json.Unmarshal([]byte(warp), &warpData) + if err != nil { + return "", err + } + + url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s/account", warpData["device_id"]) + data := fmt.Sprintf(`{"license": "%s"}`, license) + + req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(data))) + if err != nil { + return "", err + } + req.Header.Set("Authorization", "Bearer "+warpData["access_token"]) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + buffer := bytes.NewBuffer(make([]byte, 8192)) + buffer.Reset() + _, err = buffer.ReadFrom(resp.Body) + if err != nil { + return "", err + } + + warpData["license_key"] = license + newWarpData, err := json.MarshalIndent(warpData, "", " ") + if err != nil { + return "", err + } + s.SettingService.SetWarp(string(newWarpData)) + println(string(newWarpData)) + + return string(newWarpData), nil +}