Skip to content

Commit

Permalink
refactor code according to configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmet Turkmen <[email protected]>
  • Loading branch information
mrtrkmn committed Nov 10, 2020
1 parent 2d4f7f0 commit 32bd9aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
9 changes: 6 additions & 3 deletions grpc/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ var (
)

func main() {
configuration, err := config.InitializeConfig(configPath)
if configPath == "" {
panic("Set CONFIG_PATH environment variable correctly ! ")
}
configuration, err := config.NewConfig(configPath)
if err != nil {
panic("Configuration initialization error: " + err.Error())
}
port := strconv.FormatUint(uint64(configuration.GrpcConfig.Domain.Port), 10)
port := strconv.FormatUint(uint64(configuration.ServiceConfig.Domain.Port), 10)

lis, err := net.Listen("tcp", ":"+port)
if err != nil {
Expand All @@ -35,7 +38,7 @@ func main() {
if err != nil {
return
}
opts, err := wg.SecureConn(configuration.GrpcConfig.Tls)
opts, err := wg.SecureConn(configuration)
if err != nil {
log.Fatalf("failed to retrieve secure options %s", err.Error())
}
Expand Down
36 changes: 18 additions & 18 deletions vpn/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ type wireguard struct {
func (w *wireguard) InitializeI(ctx context.Context, r *pb.IReq) (*pb.IResp, error) {

log.Info().Msgf("Initializing interface for %s ", r.IName)
privKey, err := generatePrivateKey(w.config.WgInterface.Dir + r.IName + "_priv")
privKey, err := generatePrivateKey(w.config.WgConfig.Dir + r.IName + "_priv")
if err != nil {
return &pb.IResp{}, err
}
log.Info().Msgf("Private key is generated %s with name %s", w.config.WgInterface.Dir, r.IName)
if err := generatePublicKey(ctx, w.config.WgInterface.Dir+r.IName+"_priv", w.config.WgInterface.Dir+r.IName+"_pub"); err != nil {
log.Info().Msgf("Private key is generated %s with name %s", w.config.WgConfig.Dir, r.IName)
if err := generatePublicKey(ctx, w.config.WgConfig.Dir+r.IName+"_priv", w.config.WgConfig.Dir+r.IName+"_pub"); err != nil {
return &pb.IResp{}, err
}

Expand All @@ -47,7 +47,7 @@ func (w *wireguard) InitializeI(ctx context.Context, r *pb.IReq) (*pb.IResp, err
saveConfig: r.SaveConfig,
iName: r.IName,
}
out, err := genInterfaceConf(wgI, w.config.WgInterface.Dir)
out, err := genInterfaceConf(wgI, w.config.WgConfig.Dir)
if err != nil {
return &pb.IResp{Message: out}, err
}
Expand Down Expand Up @@ -133,29 +133,29 @@ func (w *wireguard) ListPeers(ctx context.Context, r *pb.ListPeersReq) (*pb.List
// GenPrivateKey generates PrivateKey for wireguard interface
func (w *wireguard) GenPrivateKey(ctx context.Context, r *pb.PrivKeyReq) (*pb.PrivKeyResp, error) {

_, err := generatePrivateKey(w.config.WgInterface.Dir + r.PrivateKeyName + "_priv")
_, err := generatePrivateKey(w.config.WgConfig.Dir + r.PrivateKeyName + "_priv")
if err != nil {
return &pb.PrivKeyResp{}, err
}
log.Info().Msgf("GenPrivateKey is called to generate new private key with filename %s", r.PrivateKeyName)
return &pb.PrivKeyResp{Message: "Private Key is created with name " + w.config.WgInterface.Dir + r.PrivateKeyName}, nil
return &pb.PrivKeyResp{Message: "Private Key is created with name " + w.config.WgConfig.Dir + r.PrivateKeyName}, nil
}

// GenPublicKey generates PublicKey for wireguard interface
func (w *wireguard) GenPublicKey(ctx context.Context, r *pb.PubKeyReq) (*pb.PubKeyResp, error) {
// check whether private key exists or not, if not generate one
if _, err := os.Stat(w.config.WgInterface.Dir + r.PrivKeyName + "_pub"); os.IsNotExist(err) {
if _, err := os.Stat(w.config.WgConfig.Dir + r.PrivKeyName + "_pub"); os.IsNotExist(err) {
fmt.Printf("PrivateKeyFile is not exists, creating one ... %s\n", r.PrivKeyName)
_, err := generatePrivateKey(w.config.WgInterface.Dir + r.PrivKeyName + "_priv")
_, err := generatePrivateKey(w.config.WgConfig.Dir + r.PrivKeyName + "_priv")
if err != nil {
return &pb.PubKeyResp{Message: "Error"}, fmt.Errorf("error in generation of private key %v", err)
}
}

if err := generatePublicKey(ctx, w.config.WgInterface.Dir+r.PrivKeyName+"_priv", w.config.WgInterface.Dir+r.PubKeyName+"_pub"); err != nil {
if err := generatePublicKey(ctx, w.config.WgConfig.Dir+r.PrivKeyName+"_priv", w.config.WgConfig.Dir+r.PubKeyName+"_pub"); err != nil {
return &pb.PubKeyResp{}, err
}
return &pb.PubKeyResp{Message: "Public key is generated with " + w.config.WgInterface.Dir + r.PubKeyName + " name"}, nil
return &pb.PubKeyResp{Message: "Public key is generated with " + w.config.WgConfig.Dir + r.PubKeyName + " name"}, nil
}

// GetPublicKey returns content of given PublicKey
Expand All @@ -181,17 +181,17 @@ func (w *wireguard) GetPrivateKey(ctx context.Context, req *pb.PrivKeyReq) (*pb.
return &pb.PrivKeyResp{Message: out}, nil
}

func GetCreds(conf config.CertConfig) (credentials.TransportCredentials, error) {
func GetCreds(conf config.Config) (credentials.TransportCredentials, error) {
log.Printf("Preparing credentials for RPC")

certificate, err := tls.LoadX509KeyPair(conf.CertFile, conf.CertKey)
certificate, err := tls.LoadX509KeyPair(conf.ServiceConfig.TLS.CertFile, conf.ServiceConfig.TLS.CertKey)
if err != nil {
return nil, fmt.Errorf("could not load server key pair: %s", err)
}

// Create a certificate pool from the certificate authority
certPool := x509.NewCertPool()
ca, err := ioutil.ReadFile(conf.CAFile)
ca, err := ioutil.ReadFile(conf.ServiceConfig.TLS.CAFile)
if err != nil {
return nil, fmt.Errorf("could not read ca certificate: %s", err)
}
Expand All @@ -212,10 +212,10 @@ func GetCreds(conf config.CertConfig) (credentials.TransportCredentials, error)
}

// SecureConn enables communication over secure channel
func SecureConn(conf config.CertConfig) ([]grpc.ServerOption, error) {
if conf.Enabled {
log.Info().Msgf("Conf cert-file: %s, cert-key: %s ca: %s", conf.CertFile, conf.CertKey, conf.CAFile)
creds, err := GetCreds(conf)
func SecureConn(conf *config.Config) ([]grpc.ServerOption, error) {
if conf.ServiceConfig.TLS.Enabled {
log.Info().Msgf("Conf cert-file: %s, cert-key: %s ca: %s", conf.ServiceConfig.TLS.CertFile, conf.ServiceConfig.TLS.CertKey, conf.ServiceConfig.TLS.CAFile)
creds, err := GetCreds(*conf)

if err != nil {
return []grpc.ServerOption{}, errors.New("Error on retrieving certificates: " + err.Error())
Expand All @@ -229,7 +229,7 @@ func SecureConn(conf config.CertConfig) ([]grpc.ServerOption, error) {
func InitServer(conf *config.Config) (*wireguard, error) {

gRPCServer := &wireguard{
auth: NewAuthenticator(conf.GrpcConfig.Auth.SKey, conf.GrpcConfig.Auth.AKey),
auth: NewAuthenticator(conf.ServiceConfig.Auth.SKey, conf.ServiceConfig.Auth.AKey),
config: conf,
}
return gRPCServer, nil
Expand Down
20 changes: 10 additions & 10 deletions vpn/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (

var (
// todo: fix configuration variables
configuration, _ = config.InitializeConfig(os.Getenv("CONFIG_PATH"))
configuration, _ = config.NewConfig(os.Getenv("CONFIG_PATH"))
)

type Interface struct {
Expand Down Expand Up @@ -186,7 +186,7 @@ func generatePrivateKey(privateKeyName string) (string, error) {

// getContent returns content of privateKey or publicKey depending on keyName
func getContent(keyName string) (string, error) {
out, err := ioutil.ReadFile(configuration.WgInterface.Dir + keyName)
out, err := ioutil.ReadFile(configuration.WgConfig.Dir + keyName)
if err != nil {
return "", fmt.Errorf("could not read the file %s err: %v", keyName, err)
}
Expand All @@ -200,19 +200,19 @@ func genInterfaceConf(i Interface, confPath string) (string, error) {
downRule := "iptables -D FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT;"
wgConf := fmt.Sprintf(
`
[Interface]
Address = %s
ListenPort = %d
SaveConfig = %v
PrivateKey = %s
PostUp = %siptables -t nat -A POSTROUTING -o %s -j MASQUERADE
PostDown = %siptables -t nat -D POSTROUTING -o %s -j MASQUERADE`, i.address, i.listenPort, i.saveConfig, i.privateKey,
[Interface]
Address = %s
ListenPort = %d
SaveConfig = %v
PrivateKey = %s
PostUp = %siptables -t nat -A POSTROUTING -o %s -j MASQUERADE
PostDown = %siptables -t nat -D POSTROUTING -o %s -j MASQUERADE`, i.address, i.listenPort, i.saveConfig, i.privateKey,
upRule, i.eth, downRule, i.eth)

if err := writeToFile(confPath+i.iName+".conf", wgConf); err != nil {
return "GenInterface Error: ", err
}
return i.iName + " configuration saved to " + configuration.WgInterface.Dir, nil
return i.iName + " configuration saved to " + configuration.WgConfig.Dir, nil
}

func WireGuardCmd(cmd string) ([]byte, error) {
Expand Down

0 comments on commit 32bd9aa

Please sign in to comment.