Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example call client for defatt #4

Merged
merged 1 commit into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 50 additions & 51 deletions grpc/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (c Creds) RequireTransportSecurity() bool {
}

func main() {
// change the endpoint address with your instance ip
endpointAddress := "40.127.143.202"
var conn *grpc.ClientConn
// wg is AUTH_KEY from vpn/auth.go
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
Expand All @@ -45,81 +47,78 @@ func main() {
grpc.WithInsecure(),
grpc.WithPerRPCCredentials(authCreds))

conn, err = grpc.Dial(":5353", dialOpts...)
conn, err = grpc.Dial(fmt.Sprintf("%s:5353", endpointAddress), dialOpts...)
if err != nil {
log.Fatalf("did not connect: %v", err)
}
if err == nil {
fmt.Printf("Client is connected successfully !")
}
defer conn.Close()

client := wg.NewWireguardClient(conn)
//privatekey generation example
privKeyResp, err := client.GenPrivateKey(context.Background(), &wg.PrivKeyReq{PrivateKeyName: "random_privatekey"})
if err != nil {
fmt.Println(fmt.Sprintf("Error happened in creating private key %v", err))
panic(err)
}
fmt.Println(privKeyResp.Message)

// publickey generation example
publicKeyResp, err := client.GenPublicKey(context.Background(), &wg.PubKeyReq{PrivKeyName: "random_privatekey", PubKeyName: "random_publickey"})
if err != nil {
fmt.Println(fmt.Sprintf("Error happened in creating public key %s", err.Error()))
//panic(err)
}
if publicKeyResp != nil {
fmt.Println(publicKeyResp.Message)
}
ctx := context.TODO()

privateKey, err := client.GetPrivateKey(context.Background(), &wg.PrivKeyReq{PrivateKeyName: "random_privatekey"})
_, err = client.InitializeI(ctx, &wg.IReq{
Address: "10.2.11.1/24", // this should be randomized and should not collide with lab subnet like 124.5.6.0/24
ListenPort: 51820, // this should be randomized and should not collide with any used ports by host
SaveConfig: true,
Eth: "eth0",
IName: "wg",
})
if err != nil {
fmt.Println(fmt.Sprintf("Get content of private key error %s", err.Error()))
//panic(err)
}
if privateKey != nil {
fmt.Println(privateKey.Message)
panic(err)
}

publicKey, err := client.GetPublicKey(context.Background(), &wg.PubKeyReq{PubKeyName: "random_publickey"})
//log.Info().Msg("Getting server public key...")
serverPubKey, err := client.GetPublicKey(ctx, &wg.PubKeyReq{PubKeyName: "wg", PrivKeyName: "wg"})
if err != nil {
fmt.Println(fmt.Sprintf("Get content of public key error %s", err.Error()))
//panic(err)
}
if publicKey != nil {
fmt.Println(publicKey.Message)
panic(err)
}

// insert content of privatekey in to initInterface
interfaceGenResp, err := client.InitializeI(context.Background(), &wg.IReq{
Address: "10.0.2.1/24",
ListenPort: 4000,
SaveConfig: true,
PrivateKey: privateKey.Message,
Eth: "eth0",
IName: "wg1",
resp, err := client.GenPublicKey(ctx, &wg.PubKeyReq{
PubKeyName: "client1",
PrivKeyName: "client1",
})
if err != nil {
fmt.Println(fmt.Sprintf(" Initializing interface error %v", err.Error()))

}
if interfaceGenResp != nil {
fmt.Println(interfaceGenResp.Message)
panic(err)
}
fmt.Printf(resp.Message)

nicInfoResp, err := client.GetNICInfo(context.Background(), &wg.NICInfoReq{Interface: "wg1"})
publicKey, err := client.GetPublicKey(ctx, &wg.PubKeyReq{PubKeyName: "client1"})
if err != nil {
fmt.Println(fmt.Sprintf("Getting information of interface error %s", err.Error()))
panic(err)
}
if nicInfoResp != nil {
fmt.Println(nicInfoResp.Message)
clientPrivKey, err := client.GetPrivateKey(ctx, &wg.PrivKeyReq{PrivateKeyName: "client1"})
if err != nil {
panic(err)
}

downI, err := client.ManageNIC(context.Background(), &wg.ManageNICReq{Cmd: "down", Nic: "wg1"})
peerIP := "10.2.11.3/32"

_, err = client.AddPeer(ctx, &wg.AddPReq{
Nic: "wg",
AllowedIPs: peerIP,
PublicKey: publicKey.Message,
})
if err != nil {
fmt.Println(fmt.Sprintf("down interface is failed %s", err.Error()))
panic(err)
}
fmt.Println(downI.Message)

//resp, err := client.GenPublicKey(context.Background(), &wg.PrivKeyReq{})
// change allowed ips according to vlan ip that you would like to connect

allowedIps := "10.134.130.1/24"
clientConfig := fmt.Sprintf(
`[Interface]
Address = %s
PrivateKey = %s
DNS = 1.1.1.1
MTU = 1500
[Peer]
PublicKey = %s
AllowedIps = %s
Endpoint = %s
PersistentKeepalive = 25`, peerIP, clientPrivKey.Message, serverPubKey.Message, allowedIps, fmt.Sprintf("%s:51820", endpointAddress))

fmt.Printf("Client Config \n %s ", clientConfig)
}
23 changes: 23 additions & 0 deletions scripts/wg-service.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Unit]
Description=Wireguard gRPC Service
ConditionPathExists=/home/vagrant
After=network.target

[Service]
Type=simple
User=vagrant
Group=vagrant
LimitNOFILE=1024

Restart=on-failure
RestartSec=10

WorkingDirectory=/home/vagrant
ExecStart=/home/vagrant/wg-server

StandardOutput=journal
StandardError=journal
SyslogIdentifier=wg-server

[Install]
WantedBy=multi-user.target