mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-05-10 10:21:51 +00:00
[warp] enhanced + delete option
Co-Authored-By: Alireza Ahmadi <alireza7@gmail.com>
This commit is contained in:
parent
816adfc3ea
commit
bb9b9100a8
4 changed files with 258 additions and 195 deletions
|
@ -12,6 +12,7 @@ type XraySettingController struct {
|
||||||
InboundService service.InboundService
|
InboundService service.InboundService
|
||||||
OutboundService service.OutboundService
|
OutboundService service.OutboundService
|
||||||
XrayService service.XrayService
|
XrayService service.XrayService
|
||||||
|
WarpService service.WarpService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewXraySettingController(g *gin.RouterGroup) *XraySettingController {
|
func NewXraySettingController(g *gin.RouterGroup) *XraySettingController {
|
||||||
|
@ -72,16 +73,18 @@ func (a *XraySettingController) warp(c *gin.Context) {
|
||||||
var err error
|
var err error
|
||||||
switch action {
|
switch action {
|
||||||
case "data":
|
case "data":
|
||||||
resp, err = a.XraySettingService.GetWarp()
|
resp, err = a.WarpService.GetWarpData()
|
||||||
|
case "del":
|
||||||
|
err = a.WarpService.DelWarpData()
|
||||||
case "config":
|
case "config":
|
||||||
resp, err = a.XraySettingService.GetWarpConfig()
|
resp, err = a.WarpService.GetWarpConfig()
|
||||||
case "reg":
|
case "reg":
|
||||||
skey := c.PostForm("privateKey")
|
skey := c.PostForm("privateKey")
|
||||||
pkey := c.PostForm("publicKey")
|
pkey := c.PostForm("publicKey")
|
||||||
resp, err = a.XraySettingService.RegWarp(skey, pkey)
|
resp, err = a.WarpService.RegWarp(skey, pkey)
|
||||||
case "license":
|
case "license":
|
||||||
license := c.PostForm("license")
|
license := c.PostForm("license")
|
||||||
resp, err = a.XraySettingService.SetWarpLicense(license)
|
resp, err = a.WarpService.SetWarpLicense(license)
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonObj(c, resp, err)
|
jsonObj(c, resp, err)
|
||||||
|
|
|
@ -24,19 +24,22 @@
|
||||||
<td>[[ warpModal.warpData.private_key ]]</td>
|
<td>[[ warpModal.warpData.private_key ]]</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
<a-button @click="delConfig" :loading="warpModal.confirmLoading" type="danger">{{ i18n "delete" }}</a-button>
|
||||||
<a-divider style="margin: 0;">{{ i18n "pages.xray.outbound.settings" }}</a-divider>
|
<a-divider style="margin: 0;">{{ i18n "pages.xray.outbound.settings" }}</a-divider>
|
||||||
<a-collapse style="margin: 10px 0;">
|
<a-collapse style="margin: 10px 0;">
|
||||||
<a-collapse-panel header='WARP/WARP+ License Key'>
|
<a-collapse-panel header='WARP/WARP+ License Key'>
|
||||||
<a-form :colon="false" :label-col="{ md: {span:6} }" :wrapper-col="{ md: {span:14} }">
|
<a-form :colon="false" :label-col="{ md: {span:6} }" :wrapper-col="{ md: {span:14} }">
|
||||||
<a-form-item label="Key">
|
<a-form-item label="Key">
|
||||||
<a-input v-model="warpPlus"></a-input>
|
<a-input v-model="warpPlus"></a-input>
|
||||||
<a-button @click="updateLicense(warpPlus)" :disabled="warpPlus.length<26" :loading="warpModal.confirmLoading">{{ i18n "pages.inbounds.update" }}</a-button>
|
<a-button @click="updateLicense(warpPlus)" :disabled="warpPlus.length<26"
|
||||||
|
:loading="warpModal.confirmLoading">{{ i18n "pages.inbounds.update" }}</a-button>
|
||||||
</a-form-item>
|
</a-form-item>
|
||||||
</a-form>
|
</a-form>
|
||||||
</a-collapse-panel>
|
</a-collapse-panel>
|
||||||
</a-collapse>
|
</a-collapse>
|
||||||
<a-divider style="margin: 0;">{{ i18n "pages.xray.outbound.accountInfo" }}</a-divider>
|
<a-divider style="margin: 0;">{{ i18n "pages.xray.outbound.accountInfo" }}</a-divider>
|
||||||
<a-button icon="sync" @click="getConfig" style="margin-top: 5px; margin-bottom: 10px;" :loading="warpModal.confirmLoading" type="primary">{{ i18n "info" }}</a-button>
|
<a-button icon="sync" @click="getConfig" style="margin-top: 5px; margin-bottom: 10px;"
|
||||||
|
:loading="warpModal.confirmLoading" type="primary">{{ i18n "info" }}</a-button>
|
||||||
<template v-if="!ObjectUtil.isEmpty(warpModal.warpConfig)">
|
<template v-if="!ObjectUtil.isEmpty(warpModal.warpConfig)">
|
||||||
<table style="width: 100%">
|
<table style="width: 100%">
|
||||||
<tr class="client-table-odd-row">
|
<tr class="client-table-odd-row">
|
||||||
|
@ -51,39 +54,39 @@
|
||||||
<td>Device Enabled</td>
|
<td>Device Enabled</td>
|
||||||
<td>[[ warpModal.warpConfig.enabled ]]</td>
|
<td>[[ warpModal.warpConfig.enabled ]]</td>
|
||||||
</tr>
|
</tr>
|
||||||
<template v-if="!ObjectUtil.isEmpty(warpModal.warpConfig.account)">
|
<template v-if="!ObjectUtil.isEmpty(warpModal.warpConfig.account)">
|
||||||
<tr>
|
<tr>
|
||||||
<td>Account Type</td>
|
<td>Account Type</td>
|
||||||
<td>[[ warpModal.warpConfig.account.account_type ]]</td>
|
<td>[[ warpModal.warpConfig.account.account_type ]]</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr class="client-table-odd-row">
|
<tr class="client-table-odd-row">
|
||||||
<td>Role</td>
|
<td>Role</td>
|
||||||
<td>[[ warpModal.warpConfig.account.role ]]</td>
|
<td>[[ warpModal.warpConfig.account.role ]]</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>WARP+ Data</td>
|
<td>WARP+ Data</td>
|
||||||
<td>[[ sizeFormat(warpModal.warpConfig.account.premium_data) ]]</td>
|
<td>[[ sizeFormat(warpModal.warpConfig.account.premium_data) ]]</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr class="client-table-odd-row">
|
<tr class="client-table-odd-row">
|
||||||
<td>Quota</td>
|
<td>Quota</td>
|
||||||
<td>[[ sizeFormat(warpModal.warpConfig.account.quota) ]]</td>
|
<td>[[ sizeFormat(warpModal.warpConfig.account.quota) ]]</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr v-if="!ObjectUtil.isEmpty(warpModal.warpConfig.account.usage)">
|
<tr v-if="!ObjectUtil.isEmpty(warpModal.warpConfig.account.usage)">
|
||||||
<td>Usage</td>
|
<td>Usage</td>
|
||||||
<td>[[ sizeFormat(warpModal.warpConfig.account.usage) ]]</td>
|
<td>[[ sizeFormat(warpModal.warpConfig.account.usage) ]]</td>
|
||||||
</tr>
|
</tr>
|
||||||
</template>
|
</template>
|
||||||
</table>
|
</table>
|
||||||
<a-divider style="margin: 10px 0;">{{ i18n "pages.xray.outbound.outboundStatus" }}</a-divider>
|
<a-divider style="margin: 10px 0;">{{ i18n "pages.xray.outbound.outboundStatus" }}</a-divider>
|
||||||
<a-form :label-col="{ md: {span:8} }" :wrapper-col="{ md: {span:14} }">
|
<a-form :label-col="{ md: {span:8} }" :wrapper-col="{ md: {span:14} }">
|
||||||
<template v-if="warpOutboundIndex>=0">
|
<template v-if="warpOutboundIndex>=0">
|
||||||
<a-tag color="green" style="line-height: 31px;">{{ i18n "enabled" }}</a-tag>
|
<a-tag color="green" style="line-height: 31px;">{{ i18n "enabled" }}</a-tag>
|
||||||
<a-button @click="resetOutbound" :loading="warpModal.confirmLoading" type="danger">{{ i18n "reset" }}</a-button>
|
<a-button @click="resetOutbound" :loading="warpModal.confirmLoading" type="danger">{{ i18n "reset" }}</a-button>
|
||||||
</template>
|
</template>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<a-tag color="orange" style="line-height: 31px;">{{ i18n "disabled" }}</a-tag>
|
<a-tag color="orange" style="line-height: 31px;">{{ i18n "disabled" }}</a-tag>
|
||||||
<a-button @click="addOutbound" :loading="warpModal.confirmLoading" type="primary">{{ i18n "pages.xray.outbound.addOutbound" }}</a-button>
|
<a-button @click="addOutbound" :loading="warpModal.confirmLoading" type="primary">{{ i18n "pages.xray.outbound.addOutbound" }}</a-button>
|
||||||
</template>
|
</template>
|
||||||
</a-form-item>
|
</a-form-item>
|
||||||
</a-form>
|
</a-form>
|
||||||
</template>
|
</template>
|
||||||
|
@ -101,21 +104,20 @@
|
||||||
this.visible = true;
|
this.visible = true;
|
||||||
this.warpConfig = null;
|
this.warpConfig = null;
|
||||||
this.getData();
|
this.getData();
|
||||||
|
|
||||||
},
|
},
|
||||||
close() {
|
close() {
|
||||||
this.visible = false;
|
this.visible = false;
|
||||||
this.loading(false);
|
this.loading(false);
|
||||||
},
|
},
|
||||||
loading(loading=true) {
|
loading(loading = true) {
|
||||||
this.confirmLoading = loading;
|
this.confirmLoading = loading;
|
||||||
},
|
},
|
||||||
async getData(){
|
async getData() {
|
||||||
this.loading(true);
|
this.loading(true);
|
||||||
const msg = await HttpUtil.post('/panel/xray/warp/data');
|
const msg = await HttpUtil.post('/panel/xray/warp/data');
|
||||||
this.loading(false);
|
this.loading(false);
|
||||||
if (msg.success) {
|
if (msg.success) {
|
||||||
this.warpData = msg.obj.length>0 ? JSON.parse(msg.obj): null;
|
this.warpData = msg.obj.length > 0 ? JSON.parse(msg.obj) : null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -131,14 +133,15 @@
|
||||||
collectConfig() {
|
collectConfig() {
|
||||||
config = warpModal.warpConfig.config;
|
config = warpModal.warpConfig.config;
|
||||||
peer = config.peers[0];
|
peer = config.peers[0];
|
||||||
if(config){
|
if (config) {
|
||||||
warpModal.warpOutbound = Outbound.fromJson({
|
warpModal.warpOutbound = Outbound.fromJson({
|
||||||
tag: 'warp',
|
tag: 'warp',
|
||||||
protocol: Protocols.Wireguard,
|
protocol: Protocols.Wireguard,
|
||||||
settings: {
|
settings: {
|
||||||
mtu: 1420,
|
mtu: 1420,
|
||||||
secretKey: warpModal.warpData.private_key,
|
secretKey: warpModal.warpData.private_key,
|
||||||
address: Object.values(config.interface.addresses),
|
address: this.getAddresses(config.interface.addresses),
|
||||||
|
reserved: this.getResolved(config.client_id),
|
||||||
domainStrategy: 'ForceIP',
|
domainStrategy: 'ForceIP',
|
||||||
peers: [{
|
peers: [{
|
||||||
publicKey: peer.public_key,
|
publicKey: peer.public_key,
|
||||||
|
@ -149,10 +152,32 @@
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async register(){
|
getAddresses(addrs) {
|
||||||
|
let addresses = [];
|
||||||
|
if (addrs.v4) addresses.push(addrs.v4 + "/32");
|
||||||
|
if (addrs.v6) addresses.push(addrs.v6 + "/128");
|
||||||
|
return addresses;
|
||||||
|
},
|
||||||
|
getResolved(client_id) {
|
||||||
|
let reserved = [];
|
||||||
|
let decoded = atob(client_id);
|
||||||
|
let hexString = '';
|
||||||
|
for (let i = 0; i < decoded.length; i++) {
|
||||||
|
let hex = decoded.charCodeAt(i).toString(16);
|
||||||
|
hexString += (hex.length === 1 ? '0' : '') + hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < hexString.length; i += 2) {
|
||||||
|
let hexByte = hexString.slice(i, i + 2);
|
||||||
|
let decValue = parseInt(hexByte, 16);
|
||||||
|
reserved.push(decValue);
|
||||||
|
}
|
||||||
|
return reserved;
|
||||||
|
},
|
||||||
|
async register() {
|
||||||
warpModal.loading(true);
|
warpModal.loading(true);
|
||||||
keys = Wireguard.generateKeypair();
|
keys = Wireguard.generateKeypair();
|
||||||
const msg = await HttpUtil.post('/panel/xray/warp/reg',keys);
|
const msg = await HttpUtil.post('/panel/xray/warp/reg', keys);
|
||||||
if (msg.success) {
|
if (msg.success) {
|
||||||
resp = JSON.parse(msg.obj);
|
resp = JSON.parse(msg.obj);
|
||||||
warpModal.warpData = resp.data;
|
warpModal.warpData = resp.data;
|
||||||
|
@ -161,9 +186,9 @@
|
||||||
}
|
}
|
||||||
warpModal.loading(false);
|
warpModal.loading(false);
|
||||||
},
|
},
|
||||||
async updateLicense(l){
|
async updateLicense(l) {
|
||||||
warpModal.loading(true);
|
warpModal.loading(true);
|
||||||
const msg = await HttpUtil.post('/panel/xray/warp/license',{license: l});
|
const msg = await HttpUtil.post('/panel/xray/warp/license', { license: l });
|
||||||
if (msg.success) {
|
if (msg.success) {
|
||||||
warpModal.warpData = JSON.parse(msg.obj);
|
warpModal.warpData = JSON.parse(msg.obj);
|
||||||
warpModal.warpConfig = null;
|
warpModal.warpConfig = null;
|
||||||
|
@ -171,7 +196,7 @@
|
||||||
}
|
}
|
||||||
warpModal.loading(false);
|
warpModal.loading(false);
|
||||||
},
|
},
|
||||||
async getConfig(){
|
async getConfig() {
|
||||||
warpModal.loading(true);
|
warpModal.loading(true);
|
||||||
const msg = await HttpUtil.post('/panel/xray/warp/config');
|
const msg = await HttpUtil.post('/panel/xray/warp/config');
|
||||||
warpModal.loading(false);
|
warpModal.loading(false);
|
||||||
|
@ -180,20 +205,37 @@
|
||||||
this.collectConfig();
|
this.collectConfig();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
addOutbound(){
|
async delConfig() {
|
||||||
|
warpModal.loading(true);
|
||||||
|
const msg = await HttpUtil.post('/panel/xray/warp/del');
|
||||||
|
warpModal.loading(false);
|
||||||
|
if (msg.success) {
|
||||||
|
warpModal.warpData = null;
|
||||||
|
warpModal.warpConfig = null;
|
||||||
|
this.delOutbound();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
addOutbound() {
|
||||||
app.templateSettings.outbounds.push(warpModal.warpOutbound.toJson());
|
app.templateSettings.outbounds.push(warpModal.warpOutbound.toJson());
|
||||||
app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
|
app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
|
||||||
warpModal.close();
|
warpModal.close();
|
||||||
},
|
},
|
||||||
resetOutbound(){
|
resetOutbound() {
|
||||||
app.templateSettings.outbounds[this.warpOutboundIndex] = warpModal.warpOutbound.toJson();
|
app.templateSettings.outbounds[this.warpOutboundIndex] = warpModal.warpOutbound.toJson();
|
||||||
app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
|
app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
|
||||||
warpModal.close();
|
warpModal.close();
|
||||||
|
},
|
||||||
|
delOutbound() {
|
||||||
|
if (this.warpOutboundIndex != -1) {
|
||||||
|
app.templateSettings.outbounds.splice(this.warpOutboundIndex, 1);
|
||||||
|
app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
|
||||||
|
}
|
||||||
|
warpModal.close();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
warpOutboundIndex: {
|
warpOutboundIndex: {
|
||||||
get: function() {
|
get: function () {
|
||||||
return app.templateSettings ? app.templateSettings.outbounds.findIndex((o) => o.tag == 'warp') : -1;
|
return app.templateSettings ? app.templateSettings.outbounds.findIndex((o) => o.tag == 'warp') : -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
162
web/service/warp.go
Normal file
162
web/service/warp.go
Normal file
|
@ -0,0 +1,162 @@
|
||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
"x-ui/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WarpService struct {
|
||||||
|
SettingService
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WarpService) GetWarpData() (string, error) {
|
||||||
|
warp, err := s.SettingService.GetWarp()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return warp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WarpService) DelWarpData() error {
|
||||||
|
err := s.SettingService.SetWarp("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WarpService) 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 *WarpService) 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 := "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 {
|
||||||
|
logger.Debug("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 *WarpService) SetWarpLicense(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
|
||||||
|
}
|
|
@ -1,13 +1,8 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"x-ui/util/common"
|
"x-ui/util/common"
|
||||||
"x-ui/xray"
|
"x-ui/xray"
|
||||||
|
@ -32,142 +27,3 @@ func (s *XraySettingService) CheckXrayConfig(XrayTemplateConfig string) error {
|
||||||
}
|
}
|
||||||
return nil
|
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 := "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) SetWarpLicense(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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue