Skip to content

Commit

Permalink
add generator for scripts;
Browse files Browse the repository at this point in the history
add ui for making new nodes
  • Loading branch information
reddec committed Oct 11, 2019
1 parent 634d17f commit 1d93b6d
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Next new public nodes require increasing number of additional operations (+N ope

With simple UI (available on your VPN address with port 1655 by default)

![image](https://user-images.githubusercontent.com/6597086/66372742-5719e280-e9d9-11e9-9942-4577a47e913a.png)
![image](https://user-images.githubusercontent.com/6597086/66646721-92c2df80-ec59-11e9-90b3-153b50dd38be.png)



## Installation
Expand Down
4 changes: 2 additions & 2 deletions domain/monitor/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

174 changes: 173 additions & 1 deletion domain/monitor/assets/nodes.gotemplate
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@
display: flex;
justify-content: center;
}

.card {
margin: 16px;
padding: 5px;
min-height: 30px;
border-radius: 3px;
box-shadow: 0px 1px 3px #777777;
}

.name {
font-size: large;
font-weight: bold;
padding-top: 3px;
padding-bottom: 5px;
}

.subnet {
font-family: monospace;
}

.status-pending {
float: right;
padding: 3px;
Expand All @@ -35,6 +39,7 @@
color: #336699;
text-decoration: none;
}

.hostfile {
float: right;
padding: 3px;
Expand All @@ -45,15 +50,18 @@
color: white;
text-decoration: none;
}

.container {
display: block;
vertical-align: middle;
width: 800px;
}

.public {
font-weight: 100 !important;
color: #84a3e1;
}

.meta {
margin: 16px;
color: #84a3e1;
Expand All @@ -72,13 +80,107 @@
.meta > a:hover {
text-decoration: underline;
}

hr {
border: 1px solid #336699;
border-radius: 2px;
box-shadow: 0px 1px 2px #777777;
margin: 16px;
}

form {
border-radius: 2px;
box-shadow: 0px 1px 3px #777777;
margin: 16px;
padding: 6px;
}

label {
font-weight: bold;
font-size: small;
display: inline-block;
padding: 3px;
margin-bottom: 3px;
}

input {
display: inline-block;
border: none;
border-bottom: 1px dashed #777777;
}

.gen {
border-radius: 3px 3px 3px 3px;
padding: 6px;
background-color: #336699;
color: white;
border: none;
font-size: large;
width: 100%;
}

.footer {
text-align: center;
}

select {
display: inline-block;
border: none;
border-bottom: 1px dashed #777777;
background-color: white;
}

.err {
border-radius: 2px;
box-shadow: 0px 1px 3px #777777;
margin: 16px;
padding: 6px;
border-left: 4px solid #ff0000;
}

.warn {
border-radius: 2px;
box-shadow: 0px 1px 3px #777777;
margin: 16px;
padding: 6px;
border-left: 4px solid #ffaa00;
}

.add {
border-radius: 3px 3px 3px 3px;
padding: 6px;
background-color: #84a3e1;
color: white;
border: none;
margin: 1px;
}

</style>
</head>
<body>

<div class="container">
{{with .Error}}
<div class="err">
<h3>Error</h3>
<p>{{.}}</p>
</div>
{{end}}
{{with .Warning}}
<div class="warn">
<h3>Warning</h3>
<p>{{.}}</p>
</div>
{{end}}
{{if not .HasPublic}}
<div class="warn">
<h3>Warning</h3>
<p>no public nodes available in the network</p>
</div>
{{end}}
<div class="meta">
<a href="../" download="{{.Service.Config.Name}}">{{.Service.Config.Name}}</a>, <span class="meta-address">{{.Service.Address}}</span>
<a href="../" download="{{.Service.Config.Name}}">{{.Service.Config.Name}}</a>, <span
class="meta-address">{{.Service.Address}}</span>
</div>
{{range .Nodes}}
{{if .Fetched}}
Expand All @@ -95,6 +197,76 @@
</div>
{{end}}
{{end}}
{{if .HasPublic}}
<form method="POST">
<h2>Generate node script</h2>
<label>
Unique node name (required)
<input type="text" placeholder="Node name" pattern="[a-z0-9]+" name="name"/>
</label>
<br/>
<br/>
<label>
Platform
<select name="platform">
<option value="amd64" selected>x86-64</option>
<option value="i386">x86-32</option>
<option value="arm64">ARM64</option>
<option value="armv7">ARM-7</option>
<option value="armv6">ARM-6</option>
<option value="armv5">ARM-5</option>
</select>
</label>
<label>
Prefix
<input type="text" placeholder="x.y.z.t" name="prefix" value="172.173"/>
</label>
<label>
Mask
<input type="number" placeholder="8, 16, 24, 32" name="mask" value="16" pattern="[0-9]+" min="8"
max="32"/>
</label>
<label>
Port
<input type="number" placeholder="random" name="port" value="" pattern="[0-9]+" min="0"
max="65535"/>
</label>
<br/>
<div>
<ul id="publics">
</ul>
<button type="button" onclick="addPublic()" class="add">
Add public address
</button>
</div>
<br/>
<div class="footer">
<button type="submit" class="gen">Generate</button>
</div>
</form>
{{end}}
</div>
<script>

function addPublic() {
var li = document.createElement('li');
var inp = document.createElement('input');
li.appendChild(inp);
inp.setAttribute("type", "text");
inp.setAttribute("name", "public");
inp.setAttribute("placeholder", "public address");

var btn = document.createElement("button");
li.append(btn);
btn.setAttribute("type", "button");
btn.setAttribute("class", "add");
btn.innerText = "remove";
btn.onclick = function () {
document.getElementById("publics").removeChild(li);
};

document.getElementById("publics").appendChild(li);
}
</script>
</body>
</html>
53 changes: 50 additions & 3 deletions domain/monitor/http_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package monitor
import (
"bytes"
"github.com/gin-gonic/gin"
"github.com/reddec/tinc-boot/domain/generator"
"github.com/reddec/tinc-boot/types"
"html/template"
"io/ioutil"
"log"
"net/http"
"path/filepath"
)
Expand All @@ -20,6 +22,7 @@ func (ms *service) createAPI() *gin.Engine {

engine.GET("/", ms.apiServeHostFile)
engine.GET("/ui", ms.apiUiNodes)
engine.POST("/ui", ms.apiUiAddNode)
engine.POST("/rpc/watch", ms.apiWatchNode)
engine.POST("/rpc/forget", ms.apiForgetNode)
engine.POST("/rpc/kill", ms.apiKillNode)
Expand Down Expand Up @@ -96,12 +99,56 @@ func (ms *service) apiGetNodeFile(gctx *gin.Context) {
gctx.File(filepath.Join(ms.cfg.Hosts(), node))
}

func (ms *service) apiUiAddNode(gctx *gin.Context) {
var params = generator.Config{
Network: ms.cfg.Network(),
}
if err := gctx.Bind(&params); err != nil {
return
}
assembly, err := params.Generate(ms.cfg.Dir)
if err != nil {
ms.renderMainPage(gctx, err, "")
return
}
var atLeastOnePublic bool
for _, node := range ms.nodes.Copy() {
if err := node.Client().PushNodeFile(params.Name, assembly.PublicKey); err != nil {
log.Println("UI", err)
} else if node.Public {
atLeastOnePublic = true
}
}

if !atLeastOnePublic {
ms.renderMainPage(gctx, nil, "can't distribute even to one public node")
return
}

gctx.Header("Content-Disposition", "attachment; filename=\""+params.Name+".sh\"")
gctx.Data(http.StatusOK, "application/bash", assembly.Script)
}

//go:generate go-bindata -o assets.go -pkg monitor --prefix assets/ assets/
func (ms *service) apiUiNodes(gctx *gin.Context) {
nodes := ms.nodes.Copy()
ms.renderMainPage(gctx, nil, "")
}

func (ms *service) renderMainPage(gctx *gin.Context, responseErr error, warn string) {
list := ms.nodes.Copy()
var hasPublic bool
for _, n := range list {
if n.Public {
hasPublic = true
break
}
}
ms.renderTemplate(gctx, "nodes.gotemplate", gin.H{
"Nodes": nodes,
"Service": ms,
"Nodes": list,
"Service": ms,
"Error": responseErr,
"Warning": warn,
"HasPublic": hasPublic,
})
}

Expand Down
Loading

0 comments on commit 1d93b6d

Please sign in to comment.