Files
umbrix-libcore/config/warp_account.go

60 lines
1.5 KiB
Go
Raw Normal View History

2024-02-18 12:31:17 +03:30
package config
2024-02-20 22:15:52 +03:30
import (
"encoding/json"
"github.com/bepass-org/wireguard-go/warp"
)
2024-02-18 12:31:17 +03:30
type WarpAccount struct {
AccountID string `json:"account-id"`
AccessToken string `json:"access-token"`
}
2024-02-20 22:15:52 +03:30
type WarpWireguardConfig struct {
PrivateKey string `json:"private-key"`
LocalAddressIPv4 string `json:"local-address-ipv4"`
LocalAddressIPv6 string `json:"local-address-ipv6"`
PeerPublicKey string `json:"peer-public-key"`
}
func (wg WarpWireguardConfig) ToWireguardConfig() warp.WireguardConfig {
return warp.WireguardConfig{
PrivateKey: wg.PrivateKey,
LocalAddressIPv4: wg.LocalAddressIPv4,
LocalAddressIPv6: wg.LocalAddressIPv6,
PeerPublicKey: wg.PeerPublicKey,
}
}
type WarpGenerationResponse struct {
WarpAccount
Log string `json:"log"`
Config WarpWireguardConfig `json:"config"`
}
2024-02-18 12:31:17 +03:30
func GenerateWarpAccount(licenseKey string, accountId string, accessToken string) (string, error) {
2024-02-20 22:15:52 +03:30
account, log, wg, err := GenerateWarpInfo(licenseKey, accountId, accessToken)
2024-02-18 12:31:17 +03:30
if err != nil {
return "", err
}
2024-02-20 22:15:52 +03:30
2024-02-18 12:31:17 +03:30
warpAccount := WarpAccount{
2024-02-20 22:15:52 +03:30
AccountID: account.AccountID,
AccessToken: account.AccessToken,
2024-02-18 12:31:17 +03:30
}
2024-02-20 22:15:52 +03:30
warpConfig := WarpWireguardConfig{
PrivateKey: wg.PrivateKey,
LocalAddressIPv4: wg.LocalAddressIPv4,
LocalAddressIPv6: wg.LocalAddressIPv6,
PeerPublicKey: wg.PeerPublicKey,
}
response := WarpGenerationResponse{warpAccount, log, warpConfig}
responseJson, err := json.Marshal(response)
2024-02-18 12:31:17 +03:30
if err != nil {
return "", err
}
2024-02-20 22:15:52 +03:30
return string(responseJson), nil
2024-02-18 12:31:17 +03:30
}