Skip to content

Commit

Permalink
Port allocation bug removed and added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dikshabagdi committed Jun 5, 2021
1 parent 4844d35 commit ebddbdd
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 406 deletions.
11 changes: 4 additions & 7 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ SERVER = 0.0.0.0
PORT = 9080
APP_CONF_DIR = ./conf

CADDY_DOMAIN = webtun.mydomain.com
NGINX_DOMAIN = sshtun.mydomain.com

# Caddy Specifications
CADDY_CONF_DIR = /etc/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_INTERFACE_NAME = sites-available/tunnel.mydomain.com

# Port values
CADDY_UPPER_RANGE = 9000
CADDY_LOWER_RANGE = 8000
NGINX_DOMAIN = sshtun.mydomain.com
NGINX_UPPER_RANGE = 7000
NGINX_LOWER_RANGE = 6000
62 changes: 37 additions & 25 deletions api/v1/caddy/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,47 @@ func addTunnel(c *gin.Context) {
// port allocation
max, _ := strconv.Atoi(os.Getenv("CADDY_UPPER_RANGE"))
min, _ := strconv.Atoi(os.Getenv("CADDY_LOWER_RANGE"))
port, err := core.GetPort(max, min)
if err != nil {
panic(err)
}

// check validity of tunnel name and port
value, msg, err := middleware.IsValidWeb(name, port)
if err != nil {
resp = util.Message(500, "Server error, Try after some time or Contact Admin...")
c.JSON(http.StatusOK, resp)
} else if value == -1 {
resp = util.Message(404, msg)
c.JSON(http.StatusBadRequest, resp)
} 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("CADDY_DOMAIN")

//to add tunnel config
err := middleware.AddWebTunnel(data)
for {
port, err := core.GetPort(max, min)
if err != nil {
panic(err)
}

// check validity of tunnel name and port
value, msg, err := middleware.IsValidWeb(name, port)

if err != nil {
resp = util.Message(500, "Server error, Try after some time or Contact Admin...")
c.JSON(http.StatusInternalServerError, resp)
} else {
resp = util.MessageTunnel(200, data)
c.JSON(http.StatusOK, resp)
break
} else if value == -1 {
if msg == "Port Already in use" {
continue
}

resp = util.Message(404, msg)
c.JSON(http.StatusBadRequest, resp)
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("CADDY_DOMAIN")

//to add tunnel config
err := middleware.AddWebTunnel(data)
if err != nil {
resp = util.Message(500, "Server error, Try after some time or Contact Admin...")
c.JSON(http.StatusInternalServerError, resp)
break
} else {
resp = util.MessageTunnel(200, data)
c.JSON(http.StatusOK, resp)
break
}
}
}
}
Expand Down
59 changes: 35 additions & 24 deletions api/v1/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,45 @@ func addTunnel(c *gin.Context) {
// port allocation
max, _ := strconv.Atoi(os.Getenv("NGINX_UPPER_RANGE"))
min, _ := strconv.Atoi(os.Getenv("NGINX_LOWER_RANGE"))
port, err := core.GetPort(max, min)
if err != nil {
panic(err)
}

value, msg, err := middleware.IsValidSSH(name, port)
if err != nil {
resp = util.Message(500, "Server error, Try after some time or Contact Admin...")
c.JSON(http.StatusOK, resp)
} else if value == -1 {
resp = util.Message(404, msg)
c.JSON(http.StatusBadRequest, resp)
} 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)
for {
port, err := core.GetPort(max, min)
if err != nil {
panic(err)
}

value, msg, err := middleware.IsValidSSH(name, port)
if err != nil {
resp = util.Message(500, "Server error, Try after some time or Contact Admin...")
c.JSON(http.StatusInternalServerError, resp)
} else {
resp = util.MessageTunnel(200, data)
c.JSON(http.StatusOK, resp)
break
} else if value == -1 {
if msg == "Port Already in use" {
continue
}

resp = util.Message(404, msg)
c.JSON(http.StatusBadRequest, resp)
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 {
resp = util.Message(500, "Server error, Try after some time or Contact Admin...")
c.JSON(http.StatusInternalServerError, resp)
break
} else {
resp = util.MessageTunnel(200, data)
c.JSON(http.StatusOK, resp)
break
}
}
}
}
Expand Down
34 changes: 20 additions & 14 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,36 @@ import (
"github.com/TheLazarusNetwork/LazarusTunnel/util"
)

var AppConfDir = "./conf"
var CaddyJSON = "caddy.json"
var NginxJSON = "nginx.json"

var CaddyConfDir = os.Getenv("CADDY_CONF_DIR")
var CaddyFile = os.Getenv("CADDY_INTERFACE_NAME")

var NginxConfDir = os.Getenv("NGINX_CONF_DIR")
var NginxFile = os.Getenv("NGINX_INTERFACE_NAME")

//Init initializes json file for caddy and nginx
func Init() {
//caddy.json path
path := filepath.Join(os.Getenv("APP_CONF_DIR"), "caddy.json")

path := filepath.Join(AppConfDir, CaddyJSON)
//check if exists
if !util.FileExists(path) {
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
util.CheckError("caddy.json error: ", err)

_, err = file.Write([]byte("[]"))
util.CheckError("caddy.json error: ", err)
err := util.CreateJSONFile(path)
if err != nil {
util.CheckError("caddy.json error: ", err)
}
}

//nginx.json path
path = filepath.Join(os.Getenv("APP_CONF_DIR"), "nginx.json")

path = filepath.Join(os.Getenv("APP_CONF_DIR"), NginxJSON)
//check if exists
if !util.FileExists(path) {
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
util.CheckError("nginx.json error: ", err)

_, err = file.Write([]byte("[]"))
util.CheckError("nginx.json error: ", err)
err := util.CreateJSONFile(path)
if err != nil {
util.CheckError("nginx.json error: ", err)
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ func init() {
}
}

// initialize json files and update config files
core.Init()
middleware.Init()
middleware.UpdateCaddyConfig()
middleware.UpdateNginxConfig()
}

func main() {
log.WithFields(util.StandardFields).Infof("Starting WalletServices Version: %s", util.Version)
log.WithFields(util.StandardFields).Infof("Starting TunnelServices Version: %s", util.Version)

if os.Getenv("RUNTYPE") == "debug" {
// set gin release debug
Expand Down
Loading

0 comments on commit ebddbdd

Please sign in to comment.