Prechádzať zdrojové kódy

feat(show-user-ip): show user's vpn ip in cli

Closes #16.
Mustafa Arici 8 rokov pred
rodič
commit
12e4952025
6 zmenil súbory, kde vykonal 58 pridanie a 31 odobranie
  1. 1 0
      api/rpc.go
  2. 2 2
      cmd/ovpm/main.go
  3. 32 23
      pb/user.pb.go
  4. 1 0
      pb/user.proto
  5. 21 0
      user.go
  6. 1 6
      vpn.go

+ 1 - 0
api/rpc.go

@@ -27,6 +27,7 @@ func (s *UserService) List(ctx context.Context, req *pb.UserListRequest) (*pb.Us
 			ServerSerialNumber: user.GetServerSerialNumber(),
 			Username:           user.GetUsername(),
 			CreatedAt:          user.GetCreatedAt(),
+			IPNet:              user.GetIPNet(),
 		})
 	}
 

+ 2 - 2
cmd/ovpm/main.go

@@ -66,10 +66,10 @@ func main() {
 							return err
 						}
 						table := tablewriter.NewWriter(os.Stdout)
-						table.SetHeader([]string{"#", "username", "created at", "valid crt"})
+						table.SetHeader([]string{"#", "username", "ip", "created at", "valid crt"})
 						//table.SetBorder(false)
 						for i, user := range resp.Users {
-							data := []string{fmt.Sprintf("%v", i+1), user.Username, user.CreatedAt, fmt.Sprintf("%t", user.ServerSerialNumber == server.SerialNumber)}
+							data := []string{fmt.Sprintf("%v", i+1), user.Username, user.IPNet, user.CreatedAt, fmt.Sprintf("%t", user.ServerSerialNumber == server.SerialNumber)}
 							table.Append(data)
 						}
 						table.Render()

+ 32 - 23
pb/user.pb.go

@@ -144,6 +144,7 @@ type UserResponse_User struct {
 	ServerSerialNumber string `protobuf:"bytes,2,opt,name=ServerSerialNumber" json:"ServerSerialNumber,omitempty"`
 	Cert               string `protobuf:"bytes,3,opt,name=Cert" json:"Cert,omitempty"`
 	CreatedAt          string `protobuf:"bytes,4,opt,name=CreatedAt" json:"CreatedAt,omitempty"`
+	IPNet              string `protobuf:"bytes,5,opt,name=IPNet" json:"IPNet,omitempty"`
 }
 
 func (m *UserResponse_User) Reset()                    { *m = UserResponse_User{} }
@@ -179,6 +180,13 @@ func (m *UserResponse_User) GetCreatedAt() string {
 	return ""
 }
 
+func (m *UserResponse_User) GetIPNet() string {
+	if m != nil {
+		return m.IPNet
+	}
+	return ""
+}
+
 type UserGenConfigResponse struct {
 	ClientConfig string `protobuf:"bytes,1,opt,name=ClientConfig" json:"ClientConfig,omitempty"`
 }
@@ -413,27 +421,28 @@ var _UserService_serviceDesc = grpc.ServiceDesc{
 func init() { proto.RegisterFile("user.proto", fileDescriptor0) }
 
 var fileDescriptor0 = []byte{
-	// 351 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0xd1, 0x4e, 0xea, 0x40,
-	0x10, 0xbd, 0x85, 0x42, 0x2e, 0x03, 0x89, 0x30, 0x42, 0xb2, 0x36, 0x3e, 0x90, 0x7d, 0x22, 0x31,
-	0x29, 0x11, 0x1e, 0x7d, 0xd2, 0x9a, 0xf8, 0xa0, 0x31, 0x06, 0xe2, 0x07, 0xb4, 0x32, 0x9a, 0x26,
-	0xb0, 0xad, 0xbb, 0x8b, 0xfc, 0x80, 0xff, 0xe1, 0xbf, 0xf8, 0x65, 0x66, 0xbb, 0x2d, 0x05, 0x52,
-	0x0d, 0x6f, 0x3b, 0xe7, 0xcc, 0x19, 0x66, 0x0e, 0xa7, 0x00, 0x6b, 0x45, 0xd2, 0x4f, 0x65, 0xa2,
-	0x13, 0xac, 0xa5, 0x11, 0xef, 0xc1, 0xc9, 0xb3, 0x22, 0xf9, 0x10, 0x2b, 0x3d, 0xa3, 0xf7, 0x35,
-	0x29, 0xcd, 0xef, 0xa1, 0x67, 0xa0, 0x40, 0x52, 0xa8, 0x29, 0x07, 0xd1, 0x83, 0xff, 0x06, 0x14,
-	0xe1, 0x8a, 0x98, 0x33, 0x74, 0x46, 0xad, 0xd9, 0xb6, 0x36, 0xdc, 0x53, 0xa8, 0xd4, 0x26, 0x91,
-	0x0b, 0x56, 0xb3, 0x5c, 0x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 0x65, 0x36, 0x2a, 0x25, 0x37, 0xd0, 0xda,
-	0xda, 0x82, 0xac, 0x68, 0x38, 0x8c, 0x88, 0x77, 0x56, 0xc1, 0x14, 0x33, 0xa2, 0x66, 0xf6, 0x8d,
-	0x4c, 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0x31, 0xff, 0x3d, 0x2c, 0x31, 0x03, 0x00, 0x00,
+	// 362 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0xd1, 0x6a, 0xea, 0x40,
+	0x10, 0xbd, 0xd1, 0x44, 0xae, 0xa3, 0x70, 0x75, 0xae, 0xc2, 0x36, 0xf4, 0x41, 0xf6, 0x49, 0x28,
+	0x44, 0xaa, 0x8f, 0x7d, 0x6a, 0x53, 0x28, 0xa5, 0x45, 0x44, 0xe9, 0x07, 0x24, 0x75, 0x5a, 0x02,
+	0x9a, 0xa4, 0xbb, 0x6b, 0xfd, 0x97, 0xbe, 0xf4, 0xb7, 0xfa, 0x39, 0x65, 0xb3, 0x89, 0x51, 0x49,
+	0x8b, 0x6f, 0x33, 0xe7, 0xcc, 0x99, 0x4c, 0x4e, 0x4e, 0x00, 0x36, 0x92, 0x84, 0x97, 0x8a, 0x44,
+	0x25, 0x58, 0x4b, 0x43, 0xde, 0x85, 0x7f, 0x4f, 0x92, 0xc4, 0x63, 0x24, 0xd5, 0x9c, 0xde, 0x36,
+	0x24, 0x15, 0x7f, 0x80, 0xae, 0x86, 0x7c, 0x41, 0x81, 0xa2, 0x1c, 0x44, 0x17, 0xfe, 0x6a, 0x30,
+	0x0e, 0xd6, 0xc4, 0xac, 0x81, 0x35, 0x6c, 0xce, 0x77, 0xbd, 0xe6, 0x66, 0x81, 0x94, 0xdb, 0x44,
+	0x2c, 0x59, 0xcd, 0x70, 0x45, 0xcf, 0x47, 0x66, 0xd9, 0x2d, 0xad, 0xe8, 0xa4, 0x65, 0xd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65, 0x62, 0x2a, 0x25, 0x37, 0xd0, 0xdc, 0xd9, 0x82, 0xac, 0x18, 0x38,
+	0x0e, 0x8e, 0x7b, 0x56, 0xc1, 0x14, 0x3b, 0xc2, 0x46, 0xf6, 0xe7, 0x4c, 0xbe, 0x03, 0x00, 0x00,
+	0xff, 0xff, 0xf5, 0x56, 0x02, 0x91, 0x47, 0x03, 0x00, 0x00,
 }

+ 1 - 0
pb/user.proto

@@ -37,6 +37,7 @@ message UserResponse {
     string ServerSerialNumber = 2;
     string Cert = 3;
     string CreatedAt = 4;
+    string IPNet = 5;
   }
 
   repeated User users = 1;

+ 21 - 0
user.go

@@ -2,6 +2,7 @@ package ovpm
 
 import (
 	"fmt"
+	"net"
 	"time"
 
 	"github.com/Sirupsen/logrus"
@@ -214,3 +215,23 @@ func (u *DBUser) GetServerSerialNumber() string {
 func (u *DBUser) GetCreatedAt() string {
 	return u.CreatedAt.Format(time.UnixDate)
 }
+
+// getIP returns user's vpn ip addr.
+func (u *DBUser) getIP() net.IP {
+	clientsNetMask := net.IPMask(net.ParseIP(_DefaultServerNetMask))
+	clientsNetPrefix := net.ParseIP(_DefaultServerNetwork)
+	clientNet := clientsNetPrefix.Mask(clientsNetMask).To4()
+	clientNet[3] = byte(u.ID)
+	return clientNet
+}
+
+// GetIPNet returns user's vpn ip network. (e.g. 192.168.0.1/24)
+func (u *DBUser) GetIPNet() string {
+	mask := net.IPMask(net.ParseIP(_DefaultServerNetMask))
+
+	ipn := net.IPNet{
+		IP:   u.getIP(),
+		Mask: mask,
+	}
+	return ipn.String()
+}

+ 1 - 6
vpn.go

@@ -7,7 +7,6 @@ import (
 	"bytes"
 	"fmt"
 	"math/big"
-	"net"
 	"os"
 	"os/exec"
 	"strings"
@@ -488,18 +487,14 @@ func emitCCD() error {
 
 	// Create and write rendered ccd data.
 	os.Mkdir(_DefaultVPNCCDPath, 0755)
-	clientsNetMask := net.IPMask(net.ParseIP(_DefaultServerNetMask))
-	clientsNetPrefix := net.ParseIP(_DefaultServerNetwork)
-	clientNet := clientsNetPrefix.Mask(clientsNetMask).To4()
 
 	counter := 2
 	for _, user := range users {
 		var result bytes.Buffer
-		clientNet[3] = byte(counter)
 		params := struct {
 			IP      string
 			NetMask string
-		}{IP: clientNet.String(), NetMask: _DefaultServerNetMask}
+		}{IP: user.getIP().String(), NetMask: _DefaultServerNetMask}
 
 		data, err := bindata.Asset("template/ccd.file.tmpl")
 		if err != nil {