local-pair-sync-prototype/app/main.go

76 lines
1.1 KiB
Go
Raw Normal View History

2021-07-09 14:47:26 +00:00
package app
import (
"bytes"
"crypto/rand"
"crypto/tls"
)
const (
KeysDir = "./keys/"
TLSCertName = KeysDir + "tls.crt"
TLSKeyName = KeysDir + "tls.key"
)
2021-10-06 15:10:44 +00:00
type Device struct {
Name string
IP string
}
var (
ds = []Device{
{"Android", "127.0.0.11"},
{"My Desktop", "127.0.0.12"},
}
)
2021-07-09 14:47:26 +00:00
type App struct {
CertPemBytes []byte
KeyPemBytes []byte
TLSConfig tls.Config
}
func (a *App) Init() error {
return a.generateKeyAndCert()
}
2021-10-06 15:10:44 +00:00
func (a *App) Run() error {
state := NewState()
for {
err := state.Perform()
if err != nil {
return err
}
err = state.ProgressCurrentStep()
if err != nil {
return err
}
}
}
2021-07-09 14:47:26 +00:00
func (a *App) generateKeyAndCert() error {
crtWriter := bytes.NewBuffer([]byte{})
keyWriter := bytes.NewBuffer([]byte{})
err := GenerateKeyAndCert(crtWriter, keyWriter)
if err != nil {
return err
}
a.KeyPemBytes = keyWriter.Bytes()
a.CertPemBytes = crtWriter.Bytes()
return nil
}
func (a *App) MakeTLS() error {
cert, err := tls.X509KeyPair(a.CertPemBytes, a.KeyPemBytes)
if err != nil {
return err
}
a.TLSConfig = tls.Config{Certificates: []tls.Certificate{cert}, Rand: rand.Reader}
return nil
}