Implement test server
This commit is contained in:
parent
b223a25055
commit
6bc1ce6679
6 changed files with 152 additions and 6 deletions
53
main.go
53
main.go
|
@ -4,22 +4,38 @@ import (
|
|||
"bytes"
|
||||
"crypto"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"crispbyte.dev/sig-auth/client"
|
||||
"crispbyte.dev/sig-auth/server"
|
||||
"github.com/opencontainers/go-digest"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func main() {
|
||||
useClient := flag.Bool("c", false, "Run client")
|
||||
|
||||
keyPath := flag.String("key", "", "Path to the private (client mode) or public (server mode) to use")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *useClient {
|
||||
runClient(keyPath)
|
||||
} else {
|
||||
runServer(keyPath)
|
||||
}
|
||||
}
|
||||
|
||||
func runClient(keyFile *string) {
|
||||
testData := map[string]string{"hello": "world"}
|
||||
json_data, _ := json.Marshal(testData)
|
||||
|
||||
keyFile := "testkey"
|
||||
|
||||
key, err := loadPrivateKey(keyFile)
|
||||
key, err := loadPrivateKey(*keyFile)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -50,11 +66,24 @@ func main() {
|
|||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var res map[string]interface{}
|
||||
out, err := io.ReadAll(resp.Body)
|
||||
|
||||
json.NewDecoder(resp.Body).Decode(&res)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(res)
|
||||
fmt.Println(resp.StatusCode)
|
||||
fmt.Println(string(out[:]))
|
||||
}
|
||||
|
||||
func runServer(keyFile *string) {
|
||||
key, err := loadPublicKey(*keyFile)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
server.Start(key)
|
||||
}
|
||||
|
||||
func loadPrivateKey(keyFile string) (crypto.PrivateKey, error) {
|
||||
|
@ -66,3 +95,15 @@ func loadPrivateKey(keyFile string) (crypto.PrivateKey, error) {
|
|||
|
||||
return ssh.ParseRawPrivateKey(keyBytes)
|
||||
}
|
||||
|
||||
func loadPublicKey(keyFile string) (crypto.PublicKey, error) {
|
||||
keyBytes, err := os.ReadFile(keyFile)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pk, _, _, _, err := ssh.ParseAuthorizedKey(keyBytes)
|
||||
|
||||
return pk.(ssh.CryptoPublicKey).CryptoPublicKey(), err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue