Initial code commit
This commit is contained in:
90
internal/wireguard/client.go
Normal file
90
internal/wireguard/client.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type ClientProfile struct {
|
||||
OS string
|
||||
Architecture string
|
||||
ConfigContent string
|
||||
ConfigPath string
|
||||
}
|
||||
|
||||
func GenerateClientProfiles(serverConfig *ServerConfig, serverIP string, count int) ([]ClientProfile, error) {
|
||||
var profiles []ClientProfile
|
||||
|
||||
platforms := []struct {
|
||||
OS string
|
||||
Architecture string
|
||||
}{
|
||||
{"linux", "amd64"},
|
||||
{"linux", "arm64"},
|
||||
{"darwin", "amd64"},
|
||||
{"darwin", "arm64"},
|
||||
{"windows", "amd64"},
|
||||
}
|
||||
|
||||
numProfiles := count
|
||||
if numProfiles <= 0 {
|
||||
numProfiles = len(platforms)
|
||||
}
|
||||
|
||||
for i := 0; i < numProfiles; i++ {
|
||||
platform := platforms[i%len(platforms)]
|
||||
|
||||
clientPrivateKey, clientPublicKey, err := GenerateClientKeyPair()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate client key pair: %w", err)
|
||||
}
|
||||
|
||||
clientAddress := fmt.Sprintf("10.0.0.%d/24", i+2)
|
||||
clientConfig := GenerateClientConfig(
|
||||
serverIP,
|
||||
serverConfig.Port,
|
||||
serverConfig.PublicKey,
|
||||
clientPrivateKey,
|
||||
clientPublicKey,
|
||||
clientAddress,
|
||||
"0.0.0.0/0",
|
||||
)
|
||||
|
||||
profile := ClientProfile{
|
||||
OS: platform.OS,
|
||||
Architecture: platform.Architecture,
|
||||
ConfigContent: clientConfig.ToConfigFile(),
|
||||
}
|
||||
|
||||
profiles = append(profiles, profile)
|
||||
}
|
||||
|
||||
return profiles, nil
|
||||
}
|
||||
|
||||
func SaveClientProfile(profile ClientProfile, outputDir string) (string, error) {
|
||||
if err := os.MkdirAll(outputDir, 0755); err != nil {
|
||||
return "", fmt.Errorf("failed to create output directory: %w", err)
|
||||
}
|
||||
|
||||
var filename string
|
||||
switch profile.OS {
|
||||
case "darwin":
|
||||
filename = fmt.Sprintf("wg-%s-%s.conf", profile.OS, profile.Architecture)
|
||||
case "linux":
|
||||
filename = fmt.Sprintf("wg-%s-%s.conf", profile.OS, profile.Architecture)
|
||||
case "windows":
|
||||
filename = fmt.Sprintf("wg-%s-%s.conf", profile.OS, profile.Architecture)
|
||||
default:
|
||||
filename = fmt.Sprintf("wg-%s-%s.conf", profile.OS, profile.Architecture)
|
||||
}
|
||||
|
||||
configPath := filepath.Join(outputDir, filename)
|
||||
|
||||
if err := os.WriteFile(configPath, []byte(profile.ConfigContent), 0600); err != nil {
|
||||
return "", fmt.Errorf("failed to write client config: %w", err)
|
||||
}
|
||||
|
||||
return configPath, nil
|
||||
}
|
||||
102
internal/wireguard/server.go
Normal file
102
internal/wireguard/server.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/crypto/curve25519"
|
||||
)
|
||||
|
||||
type ServerConfig struct {
|
||||
PrivateKey string
|
||||
PublicKey string
|
||||
Port int
|
||||
Interface string
|
||||
Address string
|
||||
}
|
||||
|
||||
type ClientConfig struct {
|
||||
PrivateKey string
|
||||
PublicKey string
|
||||
Address string
|
||||
ServerIP string
|
||||
ServerPort int
|
||||
ServerPublicKey string
|
||||
AllowedIPs string
|
||||
Endpoint string
|
||||
}
|
||||
|
||||
func GenerateServerConfig(port int, interfaceName, address string) (*ServerConfig, error) {
|
||||
privateKey, publicKey, err := generateKeyPair()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate key pair: %w", err)
|
||||
}
|
||||
|
||||
return &ServerConfig{
|
||||
PrivateKey: privateKey,
|
||||
PublicKey: publicKey,
|
||||
Port: port,
|
||||
Interface: interfaceName,
|
||||
Address: address,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (sc *ServerConfig) ToConfigFile() string {
|
||||
return fmt.Sprintf(`[Interface]
|
||||
PrivateKey = %s
|
||||
Address = %s
|
||||
ListenPort = %d
|
||||
|
||||
`, sc.PrivateKey, sc.Address, sc.Port)
|
||||
}
|
||||
|
||||
func GenerateClientConfig(serverIP string, serverPort int, serverPublicKey, clientPrivateKey, clientPublicKey, clientAddress, allowedIPs string) *ClientConfig {
|
||||
return &ClientConfig{
|
||||
PrivateKey: clientPrivateKey,
|
||||
PublicKey: clientPublicKey,
|
||||
Address: clientAddress,
|
||||
ServerIP: serverIP,
|
||||
ServerPort: serverPort,
|
||||
ServerPublicKey: serverPublicKey,
|
||||
AllowedIPs: allowedIPs,
|
||||
Endpoint: fmt.Sprintf("%s:%d", serverIP, serverPort),
|
||||
}
|
||||
}
|
||||
|
||||
func (cc *ClientConfig) ToConfigFile() string {
|
||||
return fmt.Sprintf(`[Interface]
|
||||
PrivateKey = %s
|
||||
Address = %s
|
||||
|
||||
[Peer]
|
||||
PublicKey = %s
|
||||
Endpoint = %s
|
||||
AllowedIPs = %s
|
||||
PersistentKeepalive = 25
|
||||
|
||||
`, cc.PrivateKey, cc.Address, cc.ServerPublicKey, cc.Endpoint, cc.AllowedIPs)
|
||||
}
|
||||
|
||||
func generateKeyPair() (string, string, error) {
|
||||
var privateKey [32]byte
|
||||
if _, err := rand.Read(privateKey[:]); err != nil {
|
||||
return "", "", fmt.Errorf("failed to generate private key: %w", err)
|
||||
}
|
||||
|
||||
privateKey[0] &= 248
|
||||
privateKey[31] &= 127
|
||||
privateKey[31] |= 64
|
||||
|
||||
var publicKey [32]byte
|
||||
curve25519.ScalarBaseMult(&publicKey, &privateKey)
|
||||
|
||||
privateKeyBase64 := base64.StdEncoding.EncodeToString(privateKey[:])
|
||||
publicKeyBase64 := base64.StdEncoding.EncodeToString(publicKey[:])
|
||||
|
||||
return privateKeyBase64, publicKeyBase64, nil
|
||||
}
|
||||
|
||||
func GenerateClientKeyPair() (string, string, error) {
|
||||
return generateKeyPair()
|
||||
}
|
||||
Reference in New Issue
Block a user