diff --git a/.env-example b/.env similarity index 82% rename from .env-example rename to .env index 295ec3d..8e0517d 100644 --- a/.env-example +++ b/.env @@ -5,15 +5,15 @@ PORT = 9080 APP_CONF_DIR = ./conf # Caddy Specifications -CADDY_CONF_DIR = /etc/caddy +CADDY_CONF_DIR = ./Caddy CADDY_INTERFACE_NAME = Caddyfile CADDY_DOMAIN = webtun.mydomain.com CADDY_UPPER_RANGE = 9000 CADDY_LOWER_RANGE = 8000 # NGINX Specifications -NGINX_CONF_DIR = /etc/nginx +NGINX_CONF_DIR = ./NGINX NGINX_INTERFACE_NAME = sites-available/tunnel.mydomain.com NGINX_DOMAIN = sshtun.mydomain.com NGINX_UPPER_RANGE = 7000 -NGINX_LOWER_RANGE = 6000 \ No newline at end of file +NGINX_LOWER_RANGE = 6000 diff --git a/.gitignore b/.gitignore index 1f5481c..a4db654 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,3 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib - -# Test binary, build with go test -c -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out - -# Configuration FIles -conf/ -.env \ No newline at end of file +Caddy +NGINX +vendor \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f817959 --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ + +.PHONY: proto generate +gen: + protoc --proto_path=proto proto/*.proto --go_out=api/v1/nginx --go-grpc_out=api/v1/nginx + +clean: + rm -rf server/pb/ + rm -rf client/pb/ + +.PHONY: run server +server: + go run server/main.go redis + +.PHONY: run client +client: + go run client/main.go + +install: + go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26 + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1 + sudo apt install protobuf + sudo apt install clang-format + sudo apt install grpcurl + export GO_PATH=~/go + export PATH=$PATH:/$GO_PATH/bin + go mod vendor +path: + export GO_PATH=~/go + export PATH=$PATH:/$GO_PATH/bin + +test: + rm -rf tmp && mkdir tmp + go test -cover -race serializer/*.go + +run: + go run . ./.env + +mod: + go mod tidy + go mod vendor \ No newline at end of file diff --git a/api/gRPC_api.go b/api/gRPC_api.go new file mode 100644 index 0000000..d658ef9 --- /dev/null +++ b/api/gRPC_api.go @@ -0,0 +1,74 @@ +package api + +import ( + "context" + "log" + "net" + + pb "github.com/TheLazarusNetwork/LazarusTunnel/api/v1/nginx/pb/tunnel" + "github.com/TheLazarusNetwork/LazarusTunnel/api/v2/Caddy_gRPC" + "github.com/TheLazarusNetwork/LazarusTunnel/api/v2/Nginix_gRPC" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" +) + +type server struct { + pb.UnimplementedNginxTunnelServiceServer +} + +func InitGrpc() { + listener, err := net.Listen("tcp", ":8080") + if err != nil { + panic(err) + } + + s := grpc.NewServer() + reflection.Register(s) + ser := &server{} + pb.RegisterNginxTunnelServiceServer(s, ser) + if err := s.Serve(listener); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} + +func (s *server) GetTunnels(ctx context.Context, in *pb.Empty) (*pb.GetTunnelResponse, error) { + tunnels, status, err := Nginix_gRPC.GetTunnels() + return &pb.GetTunnelResponse{Message: tunnels, Status: int32(status)}, err +} + +func (s *server) SetTunnel(ctx context.Context, in *pb.SetTunnelRequest) (*pb.SetTunnelResponse, error) { + tunnel, status, err := Nginix_gRPC.SetTunnel(in.Name) + return &pb.SetTunnelResponse{Message: tunnel, Status: int32(status)}, err +} + +func (s *server) DeleteTunnel(ctx context.Context, in *pb.SetTunnelRequest) (*pb.DeleteTunnelResponse, error) { + msg, status, err := Nginix_gRPC.DeleteTunnel(in.Name) + return &pb.DeleteTunnelResponse{Message: msg, Status: int32(status)}, err +} + +func (s *server) GetByName(ctx context.Context, in *pb.SetTunnelRequest) (*pb.SetTunnelResponse, error) { + msg, status, err := Nginix_gRPC.GetTunnelByName(in.Name) + return &pb.SetTunnelResponse{Message: msg, Status: int32(status)}, err +} + +// caddy================================================================== + +func (s *server) GetCaddyByName(ctx context.Context, in *pb.SetTunnelRequest) (*pb.SetTunnelResponse, error) { + msg, status, err := Caddy_gRPC.CaddyGetTunnelByName(in.Name) + return &pb.SetTunnelResponse{Message: msg, Status: int32(status)}, err +} + +func (s *server) GetCaddyTunnels(ctx context.Context, in *pb.Empty) (*pb.GetTunnelResponse, error) { + tunnels, status, err := Caddy_gRPC.CaddyGetTunnels() + return &pb.GetTunnelResponse{Message: tunnels, Status: int32(status)}, err +} + +func (s *server) SetCaddyTunnel(ctx context.Context, in *pb.SetTunnelRequest) (*pb.SetTunnelResponse, error) { + tunnel, status, err := Caddy_gRPC.CaddySetTunnel(in.Name) + return &pb.SetTunnelResponse{Message: tunnel, Status: int32(status)}, err +} + +func (s *server) DeleteCaddyTunnel(ctx context.Context, in *pb.SetTunnelRequest) (*pb.DeleteTunnelResponse, error) { + msg, status, err := Caddy_gRPC.CaddyDeleteTunnel(in.Name) + return &pb.DeleteTunnelResponse{Message: msg, Status: int32(status)}, err +} diff --git a/api/v1/caddy/caddy.go b/api/v1/caddy/caddy.go index 907c5d7..7ad8d6f 100644 --- a/api/v1/caddy/caddy.go +++ b/api/v1/caddy/caddy.go @@ -1,6 +1,7 @@ package caddy import ( + "fmt" "net/http" "os" "strconv" @@ -31,6 +32,7 @@ var resp map[string]interface{} func addTunnel(c *gin.Context) { //post form parameters name := strings.ToLower(c.PostForm("name")) + fmt.Println("name :", name) // port allocation max, _ := strconv.Atoi(os.Getenv("CADDY_UPPER_RANGE")) @@ -38,18 +40,31 @@ func addTunnel(c *gin.Context) { for { port, err := core.GetPort(max, min) + fmt.Println("Port caddy :", port) + + // fmt.Println("Port :", port) + fmt.Println("err :", err) if err != nil { panic(err) + + // fmt.Println("this is the panic err :",err) + } // check validity of tunnel name and port value, msg, err := middleware.IsValidWeb(name, port) + fmt.Println("caddyValue :", value) + fmt.Println("msg :", msg) + fmt.Println("err :", err) + // fmt.Println("value :", value) if err != nil { + fmt.Println("first err != nill") resp = util.Message(500, "Server error, Try after some time or Contact Admin...") - c.JSON(http.StatusOK, resp) + c.JSON(http.StatusBadGateway, resp) break } else if value == -1 { + fmt.Println("before port is already in use") if msg == "Port Already in use" { continue } @@ -157,3 +172,4 @@ func deleteTunnel(c *gin.Context) { } } + diff --git a/api/v1/nginx/nginx.go b/api/v1/nginx/nginx.go index 4d7e399..c56ad8e 100644 --- a/api/v1/nginx/nginx.go +++ b/api/v1/nginx/nginx.go @@ -1,6 +1,7 @@ package nginx import ( + "fmt" "net/http" "os" "strconv" @@ -31,6 +32,7 @@ var resp map[string]interface{} func addTunnel(c *gin.Context) { //post form parameters name := strings.ToLower(c.PostForm("name")) + fmt.Println("nameNginix :", name) // port allocation max, _ := strconv.Atoi(os.Getenv("NGINX_UPPER_RANGE")) @@ -38,16 +40,26 @@ func addTunnel(c *gin.Context) { for { port, err := core.GetPort(max, min) + fmt.Println("Port :", port) + fmt.Println("err :", err) if err != nil { panic(err) + // fmt.Println("this is the panic error", err) } value, msg, err := middleware.IsValidSSH(name, port) + + fmt.Println("valueNginix :", value, msg) + // fmt.Println("caddyNginixValue :", value, msg, err) + fmt.Println("msg :", msg) + fmt.Println("err :", err) + if err != nil { resp = util.Message(500, "Server error, Try after some time or Contact Admin...") - c.JSON(http.StatusOK, resp) + c.JSON(http.StatusBadRequest, resp) break } else if value == -1 { + if msg == "Port Already in use" { continue } @@ -91,6 +103,72 @@ func getTunnels(c *gin.Context) { } } +// func GetTunnels() ([]*tunnel.Tunnel, int, error) { +// //read all tunnel config +// tunnels, err := middleware.ReadSSHTunnels() +// if err != nil { +// err = errors.New("Server error, Try after some time or Contact Admin...") +// return nil, 500, err +// } +// tun := []*tunnel.Tunnel{} +// btunn, _ := json.Marshal(tunnels.Tunnels) +// json.Unmarshal(btunn, &tun) +// return tun, 200, nil +// } + +// func SetTunnel(nameIn string) (*tunnel.Tunnel, int, error) { +// //post form parameters +// name := strings.ToLower(nameIn) +// t := &tunnel.Tunnel{} +// // port allocation +// max, _ := strconv.Atoi(os.Getenv("NGINX_UPPER_RANGE")) +// min, _ := strconv.Atoi(os.Getenv("NGINX_LOWER_RANGE")) + +// for { +// port, err := core.GetPort(max, min) +// if err != nil { +// panic(err) +// } + +// value, msg, err := middleware.IsValidSSH(name, port) +// if err != nil { +// return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") +// break +// } else if value == -1 { +// if msg == "Port Already in use" { +// continue +// } +// return nil, 404, errors.New(msg) +// break +// } else if value == 1 { +// //create a tunnel struct object +// var data model.Tunnel +// data.Name = name +// data.Port = strconv.Itoa(port) +// data.CreatedAt = time.Now().UTC().Format(time.RFC3339) +// data.Domain = os.Getenv("NGINX_DOMAIN") + +// //to add tunnel config +// err := middleware.AddSSHTunnel(data) +// if err != nil { +// return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") +// break +// } else { +// resp = util.MessageTunnel(200, data) +// t = &tunnel.Tunnel{ +// Name: data.Name, +// Port: data.Port, +// CreatedAt: data.CreatedAt, +// Domain: data.Domain, +// Status: data.Status, +// } +// break +// } +// } +// } +// return t, 200, nil +// } + //getTunnel get specific tunnel config func getTunnel(c *gin.Context) { //get parameter @@ -155,3 +233,4 @@ func deleteTunnel(c *gin.Context) { } } + diff --git a/api/v1/nginx/pb/tunnel/tunnel.pb.go b/api/v1/nginx/pb/tunnel/tunnel.pb.go new file mode 100644 index 0000000..15b182d --- /dev/null +++ b/api/v1/nginx/pb/tunnel/tunnel.pb.go @@ -0,0 +1,343 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: tunnel.proto + +package tunnel + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +type SetTunnelRequest struct { + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *SetTunnelRequest) Reset() { *m = SetTunnelRequest{} } +func (m *SetTunnelRequest) String() string { return proto.CompactTextString(m) } +func (*SetTunnelRequest) ProtoMessage() {} +func (*SetTunnelRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_6f51ddaa7891a711, []int{0} +} + +func (m *SetTunnelRequest) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_SetTunnelRequest.Unmarshal(m, b) +} +func (m *SetTunnelRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_SetTunnelRequest.Marshal(b, m, deterministic) +} +func (m *SetTunnelRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_SetTunnelRequest.Merge(m, src) +} +func (m *SetTunnelRequest) XXX_Size() int { + return xxx_messageInfo_SetTunnelRequest.Size(m) +} +func (m *SetTunnelRequest) XXX_DiscardUnknown() { + xxx_messageInfo_SetTunnelRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_SetTunnelRequest proto.InternalMessageInfo + +func (m *SetTunnelRequest) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +type Tunnel struct { + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Port string `protobuf:"bytes,2,opt,name=port,proto3" json:"port,omitempty"` + CreatedAt string `protobuf:"bytes,3,opt,name=createdAt,proto3" json:"createdAt,omitempty"` + Domain string `protobuf:"bytes,4,opt,name=domain,proto3" json:"domain,omitempty"` + Status string `protobuf:"bytes,5,opt,name=status,proto3" json:"status,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Tunnel) Reset() { *m = Tunnel{} } +func (m *Tunnel) String() string { return proto.CompactTextString(m) } +func (*Tunnel) ProtoMessage() {} +func (*Tunnel) Descriptor() ([]byte, []int) { + return fileDescriptor_6f51ddaa7891a711, []int{1} +} + +func (m *Tunnel) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Tunnel.Unmarshal(m, b) +} +func (m *Tunnel) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Tunnel.Marshal(b, m, deterministic) +} +func (m *Tunnel) XXX_Merge(src proto.Message) { + xxx_messageInfo_Tunnel.Merge(m, src) +} +func (m *Tunnel) XXX_Size() int { + return xxx_messageInfo_Tunnel.Size(m) +} +func (m *Tunnel) XXX_DiscardUnknown() { + xxx_messageInfo_Tunnel.DiscardUnknown(m) +} + +var xxx_messageInfo_Tunnel proto.InternalMessageInfo + +func (m *Tunnel) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +func (m *Tunnel) GetPort() string { + if m != nil { + return m.Port + } + return "" +} + +func (m *Tunnel) GetCreatedAt() string { + if m != nil { + return m.CreatedAt + } + return "" +} + +func (m *Tunnel) GetDomain() string { + if m != nil { + return m.Domain + } + return "" +} + +func (m *Tunnel) GetStatus() string { + if m != nil { + return m.Status + } + return "" +} + +type GetTunnelResponse struct { + Message []*Tunnel `protobuf:"bytes,1,rep,name=message,proto3" json:"message,omitempty"` + Status int32 `protobuf:"varint,2,opt,name=status,proto3" json:"status,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *GetTunnelResponse) Reset() { *m = GetTunnelResponse{} } +func (m *GetTunnelResponse) String() string { return proto.CompactTextString(m) } +func (*GetTunnelResponse) ProtoMessage() {} +func (*GetTunnelResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_6f51ddaa7891a711, []int{2} +} + +func (m *GetTunnelResponse) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_GetTunnelResponse.Unmarshal(m, b) +} +func (m *GetTunnelResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_GetTunnelResponse.Marshal(b, m, deterministic) +} +func (m *GetTunnelResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_GetTunnelResponse.Merge(m, src) +} +func (m *GetTunnelResponse) XXX_Size() int { + return xxx_messageInfo_GetTunnelResponse.Size(m) +} +func (m *GetTunnelResponse) XXX_DiscardUnknown() { + xxx_messageInfo_GetTunnelResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_GetTunnelResponse proto.InternalMessageInfo + +func (m *GetTunnelResponse) GetMessage() []*Tunnel { + if m != nil { + return m.Message + } + return nil +} + +func (m *GetTunnelResponse) GetStatus() int32 { + if m != nil { + return m.Status + } + return 0 +} + +type SetTunnelResponse struct { + Message *Tunnel `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + Status int32 `protobuf:"varint,2,opt,name=status,proto3" json:"status,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *SetTunnelResponse) Reset() { *m = SetTunnelResponse{} } +func (m *SetTunnelResponse) String() string { return proto.CompactTextString(m) } +func (*SetTunnelResponse) ProtoMessage() {} +func (*SetTunnelResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_6f51ddaa7891a711, []int{3} +} + +func (m *SetTunnelResponse) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_SetTunnelResponse.Unmarshal(m, b) +} +func (m *SetTunnelResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_SetTunnelResponse.Marshal(b, m, deterministic) +} +func (m *SetTunnelResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_SetTunnelResponse.Merge(m, src) +} +func (m *SetTunnelResponse) XXX_Size() int { + return xxx_messageInfo_SetTunnelResponse.Size(m) +} +func (m *SetTunnelResponse) XXX_DiscardUnknown() { + xxx_messageInfo_SetTunnelResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_SetTunnelResponse proto.InternalMessageInfo + +func (m *SetTunnelResponse) GetMessage() *Tunnel { + if m != nil { + return m.Message + } + return nil +} + +func (m *SetTunnelResponse) GetStatus() int32 { + if m != nil { + return m.Status + } + return 0 +} + +type DeleteTunnelResponse struct { + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + Status int32 `protobuf:"varint,2,opt,name=status,proto3" json:"status,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *DeleteTunnelResponse) Reset() { *m = DeleteTunnelResponse{} } +func (m *DeleteTunnelResponse) String() string { return proto.CompactTextString(m) } +func (*DeleteTunnelResponse) ProtoMessage() {} +func (*DeleteTunnelResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_6f51ddaa7891a711, []int{4} +} + +func (m *DeleteTunnelResponse) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_DeleteTunnelResponse.Unmarshal(m, b) +} +func (m *DeleteTunnelResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_DeleteTunnelResponse.Marshal(b, m, deterministic) +} +func (m *DeleteTunnelResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_DeleteTunnelResponse.Merge(m, src) +} +func (m *DeleteTunnelResponse) XXX_Size() int { + return xxx_messageInfo_DeleteTunnelResponse.Size(m) +} +func (m *DeleteTunnelResponse) XXX_DiscardUnknown() { + xxx_messageInfo_DeleteTunnelResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_DeleteTunnelResponse proto.InternalMessageInfo + +func (m *DeleteTunnelResponse) GetMessage() string { + if m != nil { + return m.Message + } + return "" +} + +func (m *DeleteTunnelResponse) GetStatus() int32 { + if m != nil { + return m.Status + } + return 0 +} + +type Empty struct { + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Empty) Reset() { *m = Empty{} } +func (m *Empty) String() string { return proto.CompactTextString(m) } +func (*Empty) ProtoMessage() {} +func (*Empty) Descriptor() ([]byte, []int) { + return fileDescriptor_6f51ddaa7891a711, []int{5} +} + +func (m *Empty) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Empty.Unmarshal(m, b) +} +func (m *Empty) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Empty.Marshal(b, m, deterministic) +} +func (m *Empty) XXX_Merge(src proto.Message) { + xxx_messageInfo_Empty.Merge(m, src) +} +func (m *Empty) XXX_Size() int { + return xxx_messageInfo_Empty.Size(m) +} +func (m *Empty) XXX_DiscardUnknown() { + xxx_messageInfo_Empty.DiscardUnknown(m) +} + +var xxx_messageInfo_Empty proto.InternalMessageInfo + +func init() { + proto.RegisterType((*SetTunnelRequest)(nil), "tunnelservice.SetTunnelRequest") + proto.RegisterType((*Tunnel)(nil), "tunnelservice.Tunnel") + proto.RegisterType((*GetTunnelResponse)(nil), "tunnelservice.GetTunnelResponse") + proto.RegisterType((*SetTunnelResponse)(nil), "tunnelservice.SetTunnelResponse") + proto.RegisterType((*DeleteTunnelResponse)(nil), "tunnelservice.DeleteTunnelResponse") + proto.RegisterType((*Empty)(nil), "tunnelservice.Empty") +} + +func init() { + proto.RegisterFile("tunnel.proto", fileDescriptor_6f51ddaa7891a711) +} + +var fileDescriptor_6f51ddaa7891a711 = []byte{ + // 360 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x94, 0x5d, 0x4b, 0x3a, 0x41, + 0x14, 0xc6, 0xff, 0xfe, 0x75, 0x95, 0x3d, 0x9a, 0xe5, 0x60, 0xb1, 0x44, 0x90, 0x6c, 0x10, 0x5e, + 0x29, 0xd8, 0x27, 0xc8, 0x5e, 0x0c, 0x02, 0x09, 0x37, 0xbb, 0xa8, 0x6e, 0x56, 0xf7, 0x20, 0x82, + 0xfb, 0xd2, 0xce, 0x31, 0xf2, 0xa6, 0xcf, 0xd4, 0x47, 0x8c, 0x99, 0xf1, 0x75, 0x94, 0x2c, 0xdc, + 0xbb, 0x99, 0xe7, 0x3c, 0xf3, 0x9b, 0x67, 0x1f, 0x86, 0x85, 0x02, 0x8d, 0x83, 0x00, 0x47, 0xb5, + 0x28, 0x0e, 0x29, 0x64, 0x7b, 0x6a, 0xc7, 0x31, 0x7e, 0x1f, 0xf6, 0xd1, 0x3e, 0x87, 0x03, 0x07, + 0xe9, 0x51, 0x6a, 0x1d, 0x7c, 0x1b, 0x23, 0x27, 0xc6, 0x20, 0x13, 0xb8, 0x3e, 0x5a, 0xa9, 0x4a, + 0xaa, 0x6a, 0x76, 0xe4, 0xda, 0xfe, 0x84, 0xac, 0x32, 0x6d, 0x9a, 0x0a, 0x2d, 0x0a, 0x63, 0xb2, + 0xfe, 0x2b, 0x4d, 0xac, 0xd9, 0x09, 0x98, 0xfd, 0x18, 0x5d, 0x42, 0xef, 0x92, 0xac, 0xb4, 0x1c, + 0x2c, 0x04, 0x76, 0x04, 0x59, 0x2f, 0xf4, 0xdd, 0x61, 0x60, 0x65, 0xe4, 0x68, 0xba, 0x13, 0x3a, + 0x27, 0x97, 0xc6, 0xdc, 0x32, 0x94, 0xae, 0x76, 0xf6, 0x2b, 0x94, 0x5a, 0x8b, 0x9c, 0x3c, 0x0a, + 0x03, 0x8e, 0xac, 0x0e, 0x39, 0x1f, 0x39, 0x77, 0x07, 0x22, 0x4d, 0xba, 0x9a, 0x6f, 0x1c, 0xd6, + 0x56, 0xbe, 0xae, 0x36, 0xf5, 0xcf, 0x5c, 0x4b, 0x74, 0x91, 0xd4, 0x58, 0xa6, 0x3b, 0x3f, 0xd3, + 0x53, 0x3b, 0xd0, 0xef, 0xa0, 0x7c, 0x8d, 0x23, 0x24, 0xd4, 0x2e, 0xb0, 0x56, 0x2f, 0x30, 0xb7, + 0x93, 0x72, 0x60, 0xdc, 0xf8, 0x11, 0x4d, 0x1a, 0x5f, 0x06, 0xb0, 0xf6, 0x60, 0x18, 0x7c, 0x28, + 0xa4, 0xa3, 0x12, 0xb1, 0x5b, 0x80, 0x79, 0x4b, 0x9c, 0x95, 0xb5, 0xbc, 0xf2, 0xe8, 0x71, 0x45, + 0x53, 0xd7, 0x6a, 0xb5, 0xff, 0xb1, 0x07, 0x30, 0xe7, 0x7d, 0xb0, 0x53, 0xed, 0x80, 0xfe, 0x5e, + 0xd6, 0x88, 0xce, 0x06, 0xe2, 0x13, 0x14, 0x96, 0x3b, 0xd8, 0x0e, 0x3d, 0xd3, 0x0c, 0x9b, 0x1a, + 0x54, 0x49, 0x5b, 0x48, 0xcd, 0x49, 0x5b, 0x3c, 0xc3, 0x44, 0x92, 0xde, 0xc3, 0x7e, 0x0b, 0xe9, + 0xca, 0xf5, 0xbc, 0xc9, 0xee, 0x45, 0x76, 0xa1, 0xe8, 0xac, 0xc0, 0x92, 0xc9, 0xf8, 0x02, 0x25, + 0xd5, 0xc7, 0x9f, 0xc8, 0xbf, 0xac, 0xb4, 0x0b, 0xc5, 0x59, 0x01, 0x09, 0xf6, 0xda, 0xcc, 0x3f, + 0x9b, 0x51, 0xaf, 0xae, 0x7c, 0xbd, 0xac, 0xfc, 0x19, 0x5d, 0x7c, 0x07, 0x00, 0x00, 0xff, 0xff, + 0xed, 0x5d, 0x5e, 0x4d, 0x9c, 0x04, 0x00, 0x00, +} diff --git a/api/v1/nginx/pb/tunnel/tunnel_grpc.pb.go b/api/v1/nginx/pb/tunnel/tunnel_grpc.pb.go new file mode 100644 index 0000000..4d6dacd --- /dev/null +++ b/api/v1/nginx/pb/tunnel/tunnel_grpc.pb.go @@ -0,0 +1,359 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.12.4 +// source: tunnel.proto + +package tunnel + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// NginxTunnelServiceClient is the client API for NginxTunnelService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type NginxTunnelServiceClient interface { + GetTunnels(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*GetTunnelResponse, error) + SetTunnel(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*SetTunnelResponse, error) + DeleteTunnel(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*DeleteTunnelResponse, error) + GetByName(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*SetTunnelResponse, error) + // + GetCaddyTunnels(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*GetTunnelResponse, error) + SetCaddyTunnel(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*SetTunnelResponse, error) + DeleteCaddyTunnel(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*DeleteTunnelResponse, error) + GetCaddyByName(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*SetTunnelResponse, error) +} + +type nginxTunnelServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewNginxTunnelServiceClient(cc grpc.ClientConnInterface) NginxTunnelServiceClient { + return &nginxTunnelServiceClient{cc} +} + +func (c *nginxTunnelServiceClient) GetTunnels(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*GetTunnelResponse, error) { + out := new(GetTunnelResponse) + err := c.cc.Invoke(ctx, "/tunnelservice.NginxTunnelService/GetTunnels", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *nginxTunnelServiceClient) SetTunnel(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*SetTunnelResponse, error) { + out := new(SetTunnelResponse) + err := c.cc.Invoke(ctx, "/tunnelservice.NginxTunnelService/SetTunnel", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *nginxTunnelServiceClient) DeleteTunnel(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*DeleteTunnelResponse, error) { + out := new(DeleteTunnelResponse) + err := c.cc.Invoke(ctx, "/tunnelservice.NginxTunnelService/DeleteTunnel", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *nginxTunnelServiceClient) GetByName(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*SetTunnelResponse, error) { + out := new(SetTunnelResponse) + err := c.cc.Invoke(ctx, "/tunnelservice.NginxTunnelService/GetByName", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *nginxTunnelServiceClient) GetCaddyTunnels(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*GetTunnelResponse, error) { + out := new(GetTunnelResponse) + err := c.cc.Invoke(ctx, "/tunnelservice.NginxTunnelService/GetCaddyTunnels", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *nginxTunnelServiceClient) SetCaddyTunnel(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*SetTunnelResponse, error) { + out := new(SetTunnelResponse) + err := c.cc.Invoke(ctx, "/tunnelservice.NginxTunnelService/SetCaddyTunnel", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *nginxTunnelServiceClient) DeleteCaddyTunnel(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*DeleteTunnelResponse, error) { + out := new(DeleteTunnelResponse) + err := c.cc.Invoke(ctx, "/tunnelservice.NginxTunnelService/DeleteCaddyTunnel", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *nginxTunnelServiceClient) GetCaddyByName(ctx context.Context, in *SetTunnelRequest, opts ...grpc.CallOption) (*SetTunnelResponse, error) { + out := new(SetTunnelResponse) + err := c.cc.Invoke(ctx, "/tunnelservice.NginxTunnelService/GetCaddyByName", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// NginxTunnelServiceServer is the server API for NginxTunnelService service. +// All implementations must embed UnimplementedNginxTunnelServiceServer +// for forward compatibility +type NginxTunnelServiceServer interface { + GetTunnels(context.Context, *Empty) (*GetTunnelResponse, error) + SetTunnel(context.Context, *SetTunnelRequest) (*SetTunnelResponse, error) + DeleteTunnel(context.Context, *SetTunnelRequest) (*DeleteTunnelResponse, error) + GetByName(context.Context, *SetTunnelRequest) (*SetTunnelResponse, error) + // + GetCaddyTunnels(context.Context, *Empty) (*GetTunnelResponse, error) + SetCaddyTunnel(context.Context, *SetTunnelRequest) (*SetTunnelResponse, error) + DeleteCaddyTunnel(context.Context, *SetTunnelRequest) (*DeleteTunnelResponse, error) + GetCaddyByName(context.Context, *SetTunnelRequest) (*SetTunnelResponse, error) + mustEmbedUnimplementedNginxTunnelServiceServer() +} + +// UnimplementedNginxTunnelServiceServer must be embedded to have forward compatible implementations. +type UnimplementedNginxTunnelServiceServer struct { +} + +func (UnimplementedNginxTunnelServiceServer) GetTunnels(context.Context, *Empty) (*GetTunnelResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetTunnels not implemented") +} +func (UnimplementedNginxTunnelServiceServer) SetTunnel(context.Context, *SetTunnelRequest) (*SetTunnelResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method SetTunnel not implemented") +} +func (UnimplementedNginxTunnelServiceServer) DeleteTunnel(context.Context, *SetTunnelRequest) (*DeleteTunnelResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method DeleteTunnel not implemented") +} +func (UnimplementedNginxTunnelServiceServer) GetByName(context.Context, *SetTunnelRequest) (*SetTunnelResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetByName not implemented") +} +func (UnimplementedNginxTunnelServiceServer) GetCaddyTunnels(context.Context, *Empty) (*GetTunnelResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetCaddyTunnels not implemented") +} +func (UnimplementedNginxTunnelServiceServer) SetCaddyTunnel(context.Context, *SetTunnelRequest) (*SetTunnelResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method SetCaddyTunnel not implemented") +} +func (UnimplementedNginxTunnelServiceServer) DeleteCaddyTunnel(context.Context, *SetTunnelRequest) (*DeleteTunnelResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method DeleteCaddyTunnel not implemented") +} +func (UnimplementedNginxTunnelServiceServer) GetCaddyByName(context.Context, *SetTunnelRequest) (*SetTunnelResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetCaddyByName not implemented") +} +func (UnimplementedNginxTunnelServiceServer) mustEmbedUnimplementedNginxTunnelServiceServer() {} + +// UnsafeNginxTunnelServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to NginxTunnelServiceServer will +// result in compilation errors. +type UnsafeNginxTunnelServiceServer interface { + mustEmbedUnimplementedNginxTunnelServiceServer() +} + +func RegisterNginxTunnelServiceServer(s grpc.ServiceRegistrar, srv NginxTunnelServiceServer) { + s.RegisterService(&NginxTunnelService_ServiceDesc, srv) +} + +func _NginxTunnelService_GetTunnels_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Empty) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NginxTunnelServiceServer).GetTunnels(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tunnelservice.NginxTunnelService/GetTunnels", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NginxTunnelServiceServer).GetTunnels(ctx, req.(*Empty)) + } + return interceptor(ctx, in, info, handler) +} + +func _NginxTunnelService_SetTunnel_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SetTunnelRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NginxTunnelServiceServer).SetTunnel(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tunnelservice.NginxTunnelService/SetTunnel", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NginxTunnelServiceServer).SetTunnel(ctx, req.(*SetTunnelRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _NginxTunnelService_DeleteTunnel_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SetTunnelRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NginxTunnelServiceServer).DeleteTunnel(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tunnelservice.NginxTunnelService/DeleteTunnel", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NginxTunnelServiceServer).DeleteTunnel(ctx, req.(*SetTunnelRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _NginxTunnelService_GetByName_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SetTunnelRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NginxTunnelServiceServer).GetByName(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tunnelservice.NginxTunnelService/GetByName", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NginxTunnelServiceServer).GetByName(ctx, req.(*SetTunnelRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _NginxTunnelService_GetCaddyTunnels_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Empty) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NginxTunnelServiceServer).GetCaddyTunnels(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tunnelservice.NginxTunnelService/GetCaddyTunnels", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NginxTunnelServiceServer).GetCaddyTunnels(ctx, req.(*Empty)) + } + return interceptor(ctx, in, info, handler) +} + +func _NginxTunnelService_SetCaddyTunnel_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SetTunnelRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NginxTunnelServiceServer).SetCaddyTunnel(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tunnelservice.NginxTunnelService/SetCaddyTunnel", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NginxTunnelServiceServer).SetCaddyTunnel(ctx, req.(*SetTunnelRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _NginxTunnelService_DeleteCaddyTunnel_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SetTunnelRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NginxTunnelServiceServer).DeleteCaddyTunnel(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tunnelservice.NginxTunnelService/DeleteCaddyTunnel", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NginxTunnelServiceServer).DeleteCaddyTunnel(ctx, req.(*SetTunnelRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _NginxTunnelService_GetCaddyByName_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SetTunnelRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NginxTunnelServiceServer).GetCaddyByName(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tunnelservice.NginxTunnelService/GetCaddyByName", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NginxTunnelServiceServer).GetCaddyByName(ctx, req.(*SetTunnelRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// NginxTunnelService_ServiceDesc is the grpc.ServiceDesc for NginxTunnelService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var NginxTunnelService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "tunnelservice.NginxTunnelService", + HandlerType: (*NginxTunnelServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetTunnels", + Handler: _NginxTunnelService_GetTunnels_Handler, + }, + { + MethodName: "SetTunnel", + Handler: _NginxTunnelService_SetTunnel_Handler, + }, + { + MethodName: "DeleteTunnel", + Handler: _NginxTunnelService_DeleteTunnel_Handler, + }, + { + MethodName: "GetByName", + Handler: _NginxTunnelService_GetByName_Handler, + }, + { + MethodName: "GetCaddyTunnels", + Handler: _NginxTunnelService_GetCaddyTunnels_Handler, + }, + { + MethodName: "SetCaddyTunnel", + Handler: _NginxTunnelService_SetCaddyTunnel_Handler, + }, + { + MethodName: "DeleteCaddyTunnel", + Handler: _NginxTunnelService_DeleteCaddyTunnel_Handler, + }, + { + MethodName: "GetCaddyByName", + Handler: _NginxTunnelService_GetCaddyByName_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "tunnel.proto", +} diff --git a/api/v2/Caddy_gRPC/caddy.go b/api/v2/Caddy_gRPC/caddy.go new file mode 100644 index 0000000..45ca9f0 --- /dev/null +++ b/api/v2/Caddy_gRPC/caddy.go @@ -0,0 +1,163 @@ +package Caddy_gRPC + +import ( + "encoding/json" + "errors" + "os" + "strconv" + "strings" + "time" + + "github.com/TheLazarusNetwork/LazarusTunnel/api/v1/nginx/pb/tunnel" + "github.com/TheLazarusNetwork/LazarusTunnel/core" + "github.com/TheLazarusNetwork/LazarusTunnel/middleware" + "github.com/TheLazarusNetwork/LazarusTunnel/model" + "github.com/TheLazarusNetwork/LazarusTunnel/util" +) + +var resp map[string]interface{} + +func CaddyGetTunnelByName(nameIn string) (*tunnel.Tunnel, int, error) { + //get parameter + // name := c.Param("name") + + //read tunnel config + tunnl, err := middleware.ReadSSHTunnel(nameIn) + if err != nil { + // resp = util.Message(500, "Server error, Try after some time or Contact Admin...") + // c.JSON(http.StatusInternalServerError, resp) + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + + } + t := &tunnel.Tunnel{} + //check if tunnel exists + if tunnl.Name == "" { + resp = util.Message(404, "Tunnel Doesn't Exists") + // c.JSON(http.StatusNotFound, resp) + return nil, 404, errors.New("Tunnel Doesn't Exists") + + } else { + port, err := strconv.Atoi(tunnl.Port) + if err != nil { + util.LogError("string conv error: ", err) + // resp = util.Message(500, "Server error, Try after some time or Contact Admin...") + // c.JSON(http.StatusInternalServerError, resp) + + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + + } else { + status, err := core.ScanPort(port) + if err != nil { + // resp = util.Message(500, "Server error, Try after some time or Contact Admin...") + // c.JSON(http.StatusInternalServerError, resp) + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + + } else { + tunnl.Status = status + // resp = util.MessageTunnel(200, *tunnel) + // c.JSON(http.StatusOK, resp) + t = &tunnel.Tunnel{ + Name: tunnl.Name, + Port: tunnl.Port, + CreatedAt: tunnl.CreatedAt, + Domain: tunnl.Domain, + Status: tunnl.Status, + } + } + } + } + return t, 200, nil + +} + +func CaddyDeleteTunnel(nameIn string) (string, int, error) { + + //read tunnel config + msg := "" + tunnel, err := middleware.ReadSSHTunnel(nameIn) + if err != nil { + return "", 500, errors.New("Server error, Try after some time or Contact Admin...") + + } + + //check if tunnel exists + if tunnel.Name == "" { + return "", 400, errors.New("Tunnel Doesn't Exists") + } else { + //delete tunnel config + err = middleware.DeleteSSHTunnel(nameIn) + if err != nil { + return "", 500, errors.New("Server error, Try after some time or Contact Admin...") + + } else { + msg = "Deleted Tunnel: " + nameIn + } + } + return msg, 200, nil +} + +func CaddyGetTunnels() ([]*tunnel.Tunnel, int, error) { + //read all tunnel config + tunnels, err := middleware.ReadSSHTunnels() + if err != nil { + err = errors.New("Server error, Try after some time or Contact Admin...") + return nil, 500, err + } + tun := []*tunnel.Tunnel{} + btunn, _ := json.Marshal(tunnels.Tunnels) + json.Unmarshal(btunn, &tun) + return tun, 200, nil +} + +func CaddySetTunnel(nameIn string) (*tunnel.Tunnel, int, error) { + //post form parameters + name := strings.ToLower(nameIn) + t := &tunnel.Tunnel{} + // port allocation + max, _ := strconv.Atoi(os.Getenv("NGINX_UPPER_RANGE")) + min, _ := strconv.Atoi(os.Getenv("NGINX_LOWER_RANGE")) + + for { + port, err := core.GetPort(max, min) + if err != nil { + panic(err) + } + + value, msg, err := middleware.IsValidSSH(name, port) + if err != nil { + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + break + } else if value == -1 { + if msg == "Port Already in use" { + continue + } + return nil, 404, errors.New(msg) + break + } else if value == 1 { + //create a tunnel struct object + var data model.Tunnel + data.Name = name + data.Port = strconv.Itoa(port) + data.CreatedAt = time.Now().UTC().Format(time.RFC3339) + data.Domain = os.Getenv("NGINX_DOMAIN") + + //to add tunnel config + err := middleware.AddSSHTunnel(data) + if err != nil { + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + break + } else { + resp = util.MessageTunnel(200, data) + t = &tunnel.Tunnel{ + Name: data.Name, + Port: data.Port, + CreatedAt: data.CreatedAt, + Domain: data.Domain, + Status: data.Status, + } + break + } + } + } + return t, 200, nil +} diff --git a/api/v2/Nginix_gRPC/nginix.go b/api/v2/Nginix_gRPC/nginix.go new file mode 100644 index 0000000..d122575 --- /dev/null +++ b/api/v2/Nginix_gRPC/nginix.go @@ -0,0 +1,163 @@ +package Nginix_gRPC + +import ( + "encoding/json" + "errors" + "os" + "strconv" + "strings" + "time" + + "github.com/TheLazarusNetwork/LazarusTunnel/api/v1/nginx/pb/tunnel" + "github.com/TheLazarusNetwork/LazarusTunnel/core" + "github.com/TheLazarusNetwork/LazarusTunnel/middleware" + "github.com/TheLazarusNetwork/LazarusTunnel/model" + "github.com/TheLazarusNetwork/LazarusTunnel/util" +) + +var resp map[string]interface{} + +func DeleteTunnel(nameIn string) (string, int, error) { + + //read tunnel config + msg := "" + tunnel, err := middleware.ReadSSHTunnel(nameIn) + if err != nil { + return "", 500, errors.New("Server error, Try after some time or Contact Admin...") + + } + + //check if tunnel exists + if tunnel.Name == "" { + return "", 400, errors.New("Tunnel Doesn't Exists") + } else { + //delete tunnel config + err = middleware.DeleteSSHTunnel(nameIn) + if err != nil { + return "", 500, errors.New("Server error, Try after some time or Contact Admin...") + + } else { + msg = "Deleted Tunnel: " + nameIn + } + } + return msg, 200, nil +} + +func GetTunnelByName(nameIn string) (*tunnel.Tunnel, int, error) { + //get parameter + // name := c.Param("name") + + //read tunnel config + tunnl, err := middleware.ReadSSHTunnel(nameIn) + if err != nil { + // resp = util.Message(500, "Server error, Try after some time or Contact Admin...") + // c.JSON(http.StatusInternalServerError, resp) + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + + } + t := &tunnel.Tunnel{} + //check if tunnel exists + if tunnl.Name == "" { + resp = util.Message(404, "Tunnel Doesn't Exists") + // c.JSON(http.StatusNotFound, resp) + return nil, 404, errors.New("Tunnel Doesn't Exists") + + } else { + port, err := strconv.Atoi(tunnl.Port) + if err != nil { + util.LogError("string conv error: ", err) + // resp = util.Message(500, "Server error, Try after some time or Contact Admin...") + // c.JSON(http.StatusInternalServerError, resp) + + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + + } else { + status, err := core.ScanPort(port) + if err != nil { + // resp = util.Message(500, "Server error, Try after some time or Contact Admin...") + // c.JSON(http.StatusInternalServerError, resp) + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + + } else { + tunnl.Status = status + // resp = util.MessageTunnel(200, *tunnel) + // c.JSON(http.StatusOK, resp) + t = &tunnel.Tunnel{ + Name: tunnl.Name, + Port: tunnl.Port, + CreatedAt: tunnl.CreatedAt, + Domain: tunnl.Domain, + Status: tunnl.Status, + } + } + } + } + return t, 200, nil + +} + +func GetTunnels() ([]*tunnel.Tunnel, int, error) { + //read all tunnel config + tunnels, err := middleware.ReadSSHTunnels() + if err != nil { + err = errors.New("Server error, Try after some time or Contact Admin...") + return nil, 500, err + } + tun := []*tunnel.Tunnel{} + btunn, _ := json.Marshal(tunnels.Tunnels) + json.Unmarshal(btunn, &tun) + return tun, 200, nil +} + +func SetTunnel(nameIn string) (*tunnel.Tunnel, int, error) { + //post form parameters + name := strings.ToLower(nameIn) + t := &tunnel.Tunnel{} + // port allocation + max, _ := strconv.Atoi(os.Getenv("NGINX_UPPER_RANGE")) + min, _ := strconv.Atoi(os.Getenv("NGINX_LOWER_RANGE")) + + for { + port, err := core.GetPort(max, min) + if err != nil { + panic(err) + } + + value, msg, err := middleware.IsValidSSH(name, port) + if err != nil { + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + break + } else if value == -1 { + if msg == "Port Already in use" { + continue + } + return nil, 404, errors.New(msg) + break + } else if value == 1 { + //create a tunnel struct object + var data model.Tunnel + data.Name = name + data.Port = strconv.Itoa(port) + data.CreatedAt = time.Now().UTC().Format(time.RFC3339) + data.Domain = os.Getenv("NGINX_DOMAIN") + + //to add tunnel config + err := middleware.AddSSHTunnel(data) + if err != nil { + return nil, 500, errors.New("Server error, Try after some time or Contact Admin...") + break + } else { + resp = util.MessageTunnel(200, data) + t = &tunnel.Tunnel{ + Name: data.Name, + Port: data.Port, + CreatedAt: data.CreatedAt, + Domain: data.Domain, + Status: data.Status, + } + break + } + } + } + return t, 200, nil +} diff --git a/conf/caddy.json b/conf/caddy.json new file mode 100644 index 0000000..56983f9 --- /dev/null +++ b/conf/caddy.json @@ -0,0 +1,26 @@ +[ + { + "name": "prince2", + "port": "6081", + "createdAt": "2022-09-25T07:09:22Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "guarav", + "port": "8081", + "createdAt": "2022-09-26T15:50:48Z", + "domain": "webtun.mydomain.com" + }, + { + "name": "guarava", + "port": "8887", + "createdAt": "2022-09-26T15:55:14Z", + "domain": "webtun.mydomain.com" + }, + { + "name": "guaravaa", + "port": "8847", + "createdAt": "2022-09-26T15:57:51Z", + "domain": "webtun.mydomain.com" + } +] \ No newline at end of file diff --git a/conf/nginx.json b/conf/nginx.json new file mode 100644 index 0000000..ec718b4 --- /dev/null +++ b/conf/nginx.json @@ -0,0 +1,92 @@ +[ + { + "name": "prince2", + "port": "6081", + "createdAt": "2022-09-25T07:09:22Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "prince3", + "port": "6887", + "createdAt": "2022-09-25T07:13:13Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "prince4", + "port": "6847", + "createdAt": "2022-09-25T07:16:25Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "creta", + "port": "6059", + "createdAt": "2022-09-26T05:19:57Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "creta2", + "port": "6318", + "createdAt": "2022-09-26T05:21:00Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "creta3", + "port": "6425", + "createdAt": "2022-09-26T07:31:10Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "magic", + "port": "6540", + "createdAt": "2022-09-26T08:09:55Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "lolop", + "port": "6300", + "createdAt": "2022-09-26T09:55:32Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "ayuhs", + "port": "6694", + "createdAt": "2022-09-26T10:59:21Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "nitish", + "port": "6511", + "createdAt": "2022-09-26T11:13:42Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "abhinav", + "port": "6162", + "createdAt": "2022-09-26T11:31:52Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "granted", + "port": "6089", + "createdAt": "2022-09-26T11:39:56Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "pushpa", + "port": "6728", + "createdAt": "2022-09-26T11:41:49Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "shubhamp", + "port": "6274", + "createdAt": "2022-09-29T06:28:14Z", + "domain": "sshtun.mydomain.com" + }, + { + "name": "shubhampywei", + "port": "6445", + "createdAt": "2022-09-29T06:30:10Z", + "domain": "sshtun.mydomain.com" + } +] \ No newline at end of file diff --git a/go.mod b/go.mod index 6193752..e356b96 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,38 @@ module github.com/TheLazarusNetwork/LazarusTunnel -go 1.16 +go 1.18 require ( github.com/danielkov/gin-helmet v0.0.0-20171108135313-1387e224435e github.com/gin-contrib/cors v1.3.1 github.com/gin-contrib/static v0.0.1 github.com/gin-gonic/gin v1.7.2 + github.com/golang/protobuf v1.5.2 github.com/joho/godotenv v1.3.0 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/sirupsen/logrus v1.8.1 + google.golang.org/grpc v1.49.0 +) + +require ( + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.13.0 // indirect + github.com/go-playground/universal-translator v0.17.0 // indirect + github.com/go-playground/validator/v10 v10.4.1 // indirect + github.com/json-iterator/go v1.1.10 // indirect + github.com/leodido/go-urn v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect + github.com/stretchr/testify v1.7.0 // indirect + github.com/ugorji/go/codec v1.1.7 // indirect + golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect + golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect + golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect + golang.org/x/text v0.3.6 // indirect + google.golang.org/genproto v0.0.0-20210126160654-44e461bb6506 // indirect + google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect + gopkg.in/yaml.v2 v2.3.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 674e073..de46eca 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,14 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/danielkov/gin-helmet v0.0.0-20171108135313-1387e224435e h1:5jVSh2l/ho6ajWhSPNN84eHEdq3dp0T7+f6r3Tc6hsk= github.com/danielkov/gin-helmet v0.0.0-20171108135313-1387e224435e/go.mod h1:IJgIiGUARc4aOr4bOQ85klmjsShkEEfiRc6q/yBSfo8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/gin-contrib/cors v1.3.1 h1:doAsuITavI4IOcd0Y19U4B+O0dNWihRyX//nn4sEmgA= github.com/gin-contrib/cors v1.3.1/go.mod h1:jjEJ4268OPZUcU7k9Pm653S7lXUGcqMADzFA61xsmDk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -24,15 +30,33 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+ github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68= +github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -44,43 +68,101 @@ github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgx github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc= +golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20210126160654-44e461bb6506 h1:uLBY0yHDCj2PMQ98KWDSIDFwn9zK2zh+tgWtbvPPBjI= +google.golang.org/genproto v0.0.0-20210126160654-44e461bb6506/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.49.0 h1:WTLtQzmQori5FUH25Pq4WT22oCsv8USpQ+F6rqtsmxw= +google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go index 56f8d60..7517adc 100644 --- a/main.go +++ b/main.go @@ -19,11 +19,13 @@ import ( ) func init() { + fmt.Println("Heloo") log.SetFormatter(&log.JSONFormatter{}) + log.SetOutput(os.Stderr) log.SetLevel(log.DebugLevel) - // Get Hostname for updating Log StandardFields + // Get Hostname for updating Log StandardFi`elds HostName, err := os.Hostname() if err != nil { log.Infof("Error in getting the Hostname: %v", err) @@ -62,9 +64,10 @@ func main() { // log level info log.SetLevel(log.InfoLevel) } - + // initialize grpc + go api.InitGrpc() // creates a gin router with default middleware: logger and recovery (crash-free) middleware - ginApp := gin.Default() + ginApp := gin.Default() //rest // cors middleware config := cors.DefaultConfig() @@ -83,14 +86,13 @@ func main() { // serve static files ginApp.Use(static.Serve("/", static.LocalFile("./ui", false))) + // Apply API Routes + api.ApplyRoutes(ginApp) // no route redirect to frontend app ginApp.NoRoute(func(c *gin.Context) { c.JSON(404, gin.H{"status": 404, "message": "Invalid Endpoint Request"}) }) - // Apply API Routes - api.ApplyRoutes(ginApp) - err := ginApp.Run(fmt.Sprintf("%s:%s", os.Getenv("SERVER"), os.Getenv("PORT"))) if err != nil { log.WithFields(util.StandardFields).Fatal("Failed to Start Server") diff --git a/middleware/caddy.go b/middleware/caddy.go index 8b2ee0f..962d23f 100644 --- a/middleware/caddy.go +++ b/middleware/caddy.go @@ -2,6 +2,7 @@ package middleware import ( "encoding/json" + "fmt" "io/ioutil" "os" "path/filepath" @@ -26,8 +27,9 @@ func IsValidWeb(name string, port int) (int, string, error) { // check if name or port is already in use tunnels, err := ReadWebTunnels() + fmt.Println("isValidweb", err) if err != nil { - return -1, "", err + return -1, "Read web tunnel is responsing err MSG from /middleware/caddy.go", err } else { for _, tunnel := range tunnels.Tunnels { if tunnel.Name == name { @@ -50,12 +52,15 @@ func IsValidWeb(name string, port int) (int, string, error) { func ReadWebTunnels() (*model.Tunnels, error) { file, err := os.OpenFile(filepath.Join(os.Getenv("APP_CONF_DIR"), "caddy.json"), os.O_RDWR|os.O_APPEND, 0666) if err != nil { + fmt.Println("ReadWebTunnels( error 1) :", err) util.LogError("File Open error: ", err) return nil, err } b, err := ioutil.ReadAll(file) if err != nil { + fmt.Println("ReadWebTunnels( error 2) :", err) + util.LogError("File Read error: ", err) return nil, err } @@ -63,6 +68,8 @@ func ReadWebTunnels() (*model.Tunnels, error) { var tunnels model.Tunnels err = json.Unmarshal(b, &tunnels.Tunnels) if err != nil { + fmt.Println("ReadWebTunnels( error 3) :", err) + util.LogError("Unmarshal json error: ", err) return nil, err } @@ -111,12 +118,13 @@ func AddWebTunnel(tunnel model.Tunnel) error { err = util.WriteFile(filepath.Join(os.Getenv("APP_CONF_DIR"), "caddy.json"), inter) if err != nil { - util.LogError("File write error: ", err) + util.LogError("1 File write error: ", err) return err } err = UpdateCaddyConfig() if err != nil { + fmt.Println("err 3") return err } @@ -152,6 +160,7 @@ func DeleteWebTunnel(tunnelName string) error { err = UpdateCaddyConfig() if err != nil { + fmt.Println("err 2") return err } diff --git a/middleware/nginx.go b/middleware/nginx.go index e7659c7..c07894f 100644 --- a/middleware/nginx.go +++ b/middleware/nginx.go @@ -2,6 +2,7 @@ package middleware import ( "encoding/json" + "fmt" "io/ioutil" "os" "path/filepath" @@ -171,6 +172,7 @@ func UpdateNginxConfig() error { } for _, tunnel := range tunnels.Tunnels { + fmt.Println("tunnel: ", tunnel) _, err := template.NginxConfigTempl(tunnel) if err != nil { util.LogError("Nginx update error: ", err) diff --git a/proto/tunnel.proto b/proto/tunnel.proto new file mode 100644 index 0000000..f4ff104 --- /dev/null +++ b/proto/tunnel.proto @@ -0,0 +1,45 @@ +syntax = "proto3"; +package tunnelservice; + +option go_package = "pb/tunnel"; + +message SetTunnelRequest { + string name = 1; +} + +message Tunnel { + string name = 1; + string port = 2; + string createdAt = 3; + string domain = 4; + string status = 5; +} + +message GetTunnelResponse { + repeated Tunnel message = 1; + int32 status = 2; +} + +message SetTunnelResponse { + Tunnel message = 1; + int32 status = 2; +} +message DeleteTunnelResponse { + string message = 1; + int32 status = 2; +} +message Empty {} + + // TunnelService proto tunnel service + service NginxTunnelService { + rpc GetTunnels(Empty) returns (GetTunnelResponse) {} + rpc SetTunnel(SetTunnelRequest) returns (SetTunnelResponse) {} + rpc DeleteTunnel(SetTunnelRequest) returns(DeleteTunnelResponse){} + rpc GetByName(SetTunnelRequest) returns (SetTunnelResponse) {} + // + rpc GetCaddyTunnels(Empty) returns (GetTunnelResponse) {} + rpc SetCaddyTunnel(SetTunnelRequest) returns (SetTunnelResponse) {} + rpc DeleteCaddyTunnel(SetTunnelRequest) returns(DeleteTunnelResponse){} + rpc GetCaddyByName(SetTunnelRequest) returns (SetTunnelResponse) {} + } +