-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09a57e2
commit aef2bef
Showing
6 changed files
with
158 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/sagernet/sing-box/common/tls" | ||
"github.com/sagernet/sing-box/log" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var commandGenerateTLSKeyPair = &cobra.Command{ | ||
Use: "tls-keypair <server_name>", | ||
Short: "Generate TLS self sign key pair", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := generateTLSKeyPair(args[0]) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
commandGenerate.AddCommand(commandGenerateTLSKeyPair) | ||
} | ||
|
||
func generateTLSKeyPair(serverName string) error { | ||
privateKeyPem, publicKeyPem, err := tls.GenerateKeyPair(time.Now, serverName) | ||
if err != nil { | ||
return err | ||
} | ||
os.Stdout.WriteString(string(privateKeyPem) + "\n") | ||
os.Stdout.WriteString(string(publicKeyPem) + "\n") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/ecdh" | ||
"crypto/rand" | ||
"encoding/base64" | ||
"os" | ||
|
||
"github.com/sagernet/sing-box/log" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var commandGenerateVAPIDKeyPair = &cobra.Command{ | ||
Use: "vapid-keypair", | ||
Short: "Generate VAPID key pair", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := generateVAPIDKeyPair() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
commandGenerate.AddCommand(commandGenerateVAPIDKeyPair) | ||
} | ||
|
||
func generateVAPIDKeyPair() error { | ||
privateKey, err := ecdh.P256().GenerateKey(rand.Reader) | ||
if err != nil { | ||
return err | ||
} | ||
publicKey := privateKey.PublicKey() | ||
os.Stdout.WriteString("PrivateKey: " + base64.RawURLEncoding.EncodeToString(privateKey.Bytes()) + "\n") | ||
os.Stdout.WriteString("PublicKey: " + base64.RawURLEncoding.EncodeToString(publicKey.Bytes()) + "\n") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"os" | ||
|
||
"github.com/sagernet/sing-box/log" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||
) | ||
|
||
func init() { | ||
commandGenerate.AddCommand(commandGenerateWireGuardKeyPair) | ||
commandGenerate.AddCommand(commandGenerateRealityKeyPair) | ||
} | ||
|
||
var commandGenerateWireGuardKeyPair = &cobra.Command{ | ||
Use: "wg-keypair", | ||
Short: "Generate WireGuard key pair", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := generateWireGuardKey() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func generateWireGuardKey() error { | ||
privateKey, err := wgtypes.GeneratePrivateKey() | ||
if err != nil { | ||
return err | ||
} | ||
os.Stdout.WriteString("PrivateKey: " + privateKey.String() + "\n") | ||
os.Stdout.WriteString("PublicKey: " + privateKey.PublicKey().String() + "\n") | ||
return nil | ||
} | ||
|
||
var commandGenerateRealityKeyPair = &cobra.Command{ | ||
Use: "reality-keypair", | ||
Short: "Generate reality key pair", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := generateRealityKey() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func generateRealityKey() error { | ||
privateKey, err := wgtypes.GeneratePrivateKey() | ||
if err != nil { | ||
return err | ||
} | ||
publicKey := privateKey.PublicKey() | ||
os.Stdout.WriteString("PrivateKey: " + base64.RawURLEncoding.EncodeToString(privateKey[:]) + "\n") | ||
os.Stdout.WriteString("PublicKey: " + base64.RawURLEncoding.EncodeToString(publicKey[:]) + "\n") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters