From 4844d35ae406b4384e25aea2202a08129691df45 Mon Sep 17 00:00:00 2001 From: dikshabagdi Date: Thu, 3 Jun 2021 15:33:50 +0530 Subject: [PATCH] Updated Template and README.md --- .env-example | 2 +- .gitignore | 16 ++++++++++++++++ README.md | 39 +++++++++++++++++++++++++++++++++------ api/v1/caddy/caddy.go | 1 + api/v1/nginx/nginx.go | 1 + caddy.json | 7 ------- middleware/handlers.go | 2 ++ model/tunnel.go | 1 + nginx.json | 7 ------- template/template.go | 6 +++--- util/util.go | 2 +- 11 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 .gitignore delete mode 100644 caddy.json delete mode 100644 nginx.json diff --git a/.env-example b/.env-example index 5c34b0f..9e95b7a 100644 --- a/.env-example +++ b/.env-example @@ -2,7 +2,7 @@ RUNTYPE = debug SERVER = 0.0.0.0 PORT = 9080 -APP_CONF_DIR = ./ +APP_CONF_DIR = ./conf CADDY_DOMAIN = webtun.mydomain.com NGINX_DOMAIN = sshtun.mydomain.com diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1f5481c --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# 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 diff --git a/README.md b/README.md index 38bf113..ad38903 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,34 @@ Tunnel Services APIs to facilitate Tunnel configuration for Lazarus Network, Fun * [Fetch All Tunnels](#fetch-all-tunnels) * [Delete Tunnel](#delete-tunnel) -# Installation +# Installation Notes + +After placing the .path and .service files in /etc/systemd/system, Run: +1. ```sudo systemctl daemon-reload``` + +2. ```sudo systemctl enable caddy-watcher.path && sudo systemctl start caddy-watcher.path``` + +Created symlink /etc/systemd/system/multi-user.target.wants/caddy-watcher.path → /etc/systemd/system/caddy-watcher.path. + +3. ```sudo systemctl enable caddy-watcher.service && sudo systemctl start caddy-watcher.service``` + +Created symlink /etc/systemd/system/multi-user.target.wants/caddy-watcher.service → /etc/systemd/system/caddy-watcher.service. + +4. ```sudo systemctl enable nginx-watcher.path && sudo systemctl start nginx-watcher.path``` + +Created symlink /etc/systemd/system/multi-user.target.wants/nginx-watcher.path → /etc/systemd/system/nginx-watcher.path. + +5. ```sudo systemctl enable nginx-watcher.service && sudo systemctl start nginx-watcher.service``` + +Created symlink /etc/systemd/system/multi-user.target.wants/nginx-watcher.service → /etc/systemd/system/nginx-watcher.service. + +6. ```sudo systemctl status caddy-watcher.path``` + +7. ```sudo systemctl status caddy-watcher.service``` + +6. ```sudo systemctl status nginx-watcher.path``` + +7. ```sudo systemctl status nginx-watcher.service``` # Summary of Functions Implemented @@ -15,19 +42,19 @@ Tunnel Services APIs to facilitate Tunnel configuration for Lazarus Network, Fun Creates Web Tunnel with Caddyfile
POST - /api/v1.0/caddy
Request - name
-Response - {"message":{"name":"name1","port":"port1"},"status":200}
+Response - {"message":{"name":"name1","port":"port1", "createdAt":"timeStamp", domain":"caddyDomain"},"status":200}
### Fetch Tunnel Fetches Web Tunnel configuration and states the status of Port alloted
GET - /api/v1.0/caddy/:name
Request - name
-Response - {"message":{"name":"name1","port":"port1", "status":"inactive"},"status":200}
+Response - {"message":{"name":"name1","port":"port1", "createdAt":"timeStamp", domain":"caddyDomain", "status":"inactive"},"status":200}
### Fetch All Tunnels Fetches All Web Tunnel configurations
GET - /api/v1.0/caddy
Request - N/A
-Response - {"message":[{"name":"name1","port":"port1"}, {"name":"name2","port":"port2"}],"status":200}
+Response - {"message":[{"name":"name1","port":"port1", "createdAt":"timeStamp1", domain":"caddyDomain"}, {"name":"name2","port":"port2", "createdAt":"timeStamp2", domain":"caddyDomain"}],"status":200}
### Delete Tunnel Deletes Web Tunnel configuration from Caddyfile
@@ -41,7 +68,7 @@ Response - {"message": ”Deleted Tunnel $name”,"status":200}
Creates SSH Tunnel nginx
POST - /api/v1.0/nginx
Request - name
-Response - {"message":{"name":"name1","port":"port1"},"status":200}
+Response - {"message":{"name":"name1","port":"port1", "createdAt":"timeStamp", domain":"nginxDomain"},"status":200}
### Fetch Tunnel Fetches SSH Tunnel configuration
@@ -53,7 +80,7 @@ Response - {"message":{"name":"name1","port":"port1"},"status":200}
Fetches All SSH Tunnel configurations
GET - /api/v1.0/nginx
Request - N/A
-Response - {"message":[{"name":"name1","port":"port1"}, {"name":"name2","port":"port2"}],"status":200}
+Response - {"message":[{"name":"name1","port":"port1, "createdAt":"timeStamp1", domain":"nginxDomain""}, {"name":"name2","port":"port2", , "createdAt":"timeStamp2", domain":"nginxDomain"}],"status":200}
### Delete Tunnel Deletes SSH Tunnel configuration
diff --git a/api/v1/caddy/caddy.go b/api/v1/caddy/caddy.go index 74a1984..9646dbc 100644 --- a/api/v1/caddy/caddy.go +++ b/api/v1/caddy/caddy.go @@ -54,6 +54,7 @@ func addTunnel(c *gin.Context) { 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) diff --git a/api/v1/nginx/nginx.go b/api/v1/nginx/nginx.go index 5cc3c10..9048ef3 100644 --- a/api/v1/nginx/nginx.go +++ b/api/v1/nginx/nginx.go @@ -53,6 +53,7 @@ func addTunnel(c *gin.Context) { 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) diff --git a/caddy.json b/caddy.json deleted file mode 100644 index c774b9b..0000000 --- a/caddy.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "name": "diksha", - "port": "8081", - "createdAt": "2021-06-03T06:06:27Z" - } -] \ No newline at end of file diff --git a/middleware/handlers.go b/middleware/handlers.go index cbfb5e8..88fcaf3 100644 --- a/middleware/handlers.go +++ b/middleware/handlers.go @@ -80,6 +80,7 @@ func ReadWebTunnel(tunnelName string) (*model.Tunnel, error) { data.Name = tunnel.Name data.Port = tunnel.Port data.CreatedAt = tunnel.CreatedAt + data.Domain = tunnel.Domain data.Status = tunnel.Status break } @@ -236,6 +237,7 @@ func ReadSSHTunnel(tunnelName string) (*model.Tunnel, error) { data.Name = tunnel.Name data.Port = tunnel.Port data.CreatedAt = tunnel.CreatedAt + data.Domain = tunnel.Domain data.Status = tunnel.Status break } diff --git a/model/tunnel.go b/model/tunnel.go index ff4dd30..134880c 100644 --- a/model/tunnel.go +++ b/model/tunnel.go @@ -4,6 +4,7 @@ type Tunnel struct { Name string `json:"name"` Port string `json:"port"` CreatedAt string `json:"createdAt"` + Domain string `json:"domain"` Status string `json:"status,omitempty"` } diff --git a/nginx.json b/nginx.json deleted file mode 100644 index 02ffeee..0000000 --- a/nginx.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "name": "diksha", - "port": "6887", - "createdAt": "2021-06-03T06:06:31Z" - } -] \ No newline at end of file diff --git a/template/template.go b/template/template.go index f5dcc18..2738879 100644 --- a/template/template.go +++ b/template/template.go @@ -13,10 +13,10 @@ import ( var ( caddyTpl = ` # {{.Name}}, {{.Port}}, {{.CreatedAt}} -{{.Name}}.webtun.lazarus.network { +{{.Name}}.{{.Domain}} { reverse_proxy / 127.0.0.1:{{.Port}} log { - output file /var/log/caddy/{{.Name}}.webtun.lazarus.network.access.log { + output file /var/log/caddy/{{.Name}}.{{.Domain}}.access.log { roll_size 3MiB roll_keep 5 roll_keep_for 48h @@ -34,7 +34,7 @@ var ( # {{.Name}}, {{.Port}}, {{.CreatedAt}} server { listen 6000; - server_name {{.Name}}.sshtun.lazarus.network; + server_name {{.Name}}.{{.Domain}}; location / { proxy_pass http://127.0.0.1:{{.Port}}$request_uri; diff --git a/util/util.go b/util/util.go index 310252b..65cc0e7 100644 --- a/util/util.go +++ b/util/util.go @@ -14,7 +14,7 @@ var Version = "1.0" // StandardFields for logger var StandardFields = log.Fields{ "hostname": "HostServer", - "appname": "CaddyAPI", + "appname": "TunnelAPI", } // ReadFile file content