Skip to content

Commit

Permalink
Added some admin console changes, made time format configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomika committed May 3, 2020
1 parent eacd799 commit 9302841
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 6 deletions.
19 changes: 19 additions & 0 deletions cmd/sish.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ var (
}
)

type logWriter struct {
TimeFmt string
}

func (w logWriter) Write(bytes []byte) (int, error) {
return fmt.Printf("%v | %s", time.Now().Format(w.TimeFmt), string(bytes))
}

func init() {
cobra.OnInitialize(initConfig)

Expand Down Expand Up @@ -65,6 +73,7 @@ func init() {
rootCmd.PersistentFlags().StringP("admin-console-token", "j", "S3Cr3tP4$$W0rD", "The token to use for admin access")
rootCmd.PersistentFlags().StringP("service-console-token", "m", "", "The token to use for service access. Auto generated if empty.")
rootCmd.PersistentFlags().StringP("user-subdomain-separator", "", "", "The token to use for separating username and subdomains in a virtualhost.")
rootCmd.PersistentFlags().StringP("time-format", "", "2006/01/02 - 15:04:05", "The time format to use for both HTTP and general log messages.")

rootCmd.PersistentFlags().BoolP("bind-random-subdomains", "", true, "Whether or not to force a random subdomain")
rootCmd.PersistentFlags().BoolP("verify-origin", "", true, "Whether or not to verify origin on websocket connection")
Expand Down Expand Up @@ -111,6 +120,16 @@ func initConfig() {

viper.OnConfigChange(func(e fsnotify.Event) {
log.Println("Reloaded configuration file.")

log.SetFlags(0)
log.SetOutput(logWriter{
TimeFmt: viper.GetString("time-format"),
})
})

log.SetFlags(0)
log.SetOutput(logWriter{
TimeFmt: viper.GetString("time-format"),
})
}

Expand Down
1 change: 1 addition & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ proxy-protocol-version: "1"
redirect-root-location: https://github.com/antoniomika/sish
service-console-token: ""
ssh-address: localhost:2222
time-format: 2006/01/02 - 15:04:05
user-subdomain-separator: ""
verify-origin: true
verify-ssl: true
Expand Down
3 changes: 2 additions & 1 deletion httpmuxer/httpmuxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func StartHTTPHandler(state *utils.State) {
}

logLine := fmt.Sprintf("%v | %s |%s %3d %s| %13v | %15s |%s %-7s %s %s\n%s",
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
param.TimeStamp.Format(viper.GetString("time-format")),
param.Request.Host,
statusColor, param.StatusCode, resetColor,
param.Latency,
Expand Down Expand Up @@ -212,6 +212,7 @@ func StartHTTPHandler(state *utils.State) {

data, err := json.Marshal(map[string]interface{}{
"startTime": startTime,
"startTimePretty": startTime.Format(viper.GetString("time-format")),
"currentTime": currentTime,
"requestIP": c.ClientIP(),
"requestTime": diffTime.Round(roundTime).String(),
Expand Down
2 changes: 1 addition & 1 deletion sshmuxer/sshmuxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func Start() {
log.Println(key, value)
return true
})
log.Print("========End==========\n\n")
log.Print("========End==========\n")

time.Sleep(2 * time.Second)
}
Expand Down
4 changes: 3 additions & 1 deletion templates/console.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<table id="request-table" class="table table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">Date</th>
<th scope="col">Method</th>
<th scope="col">Path</th>
<th scope="col">Status</th>
<th scope="col">Time</th>
<th scope="col">Duration</th>
</tr>
</thead>
<tbody id="request-list">
Expand Down Expand Up @@ -57,6 +58,7 @@

$("#request-list").prepend(`
<tr id="row-timestamp-${tsPart}" class="request-row">
<th scope="row">${requestData.startTimePretty}</th>
<th scope="row">${requestData.requestMethod}</th>
<td>${requestData.requestUrl.Path}</td>
<td>${requestData.responseStatus}</td>
Expand Down
4 changes: 4 additions & 0 deletions templates/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<!--
<a class="nav-link" href="/_sish/console">Home</a>
-->
</li>
</ul>
<ul class="navbar-nav flex-row ml-md-auto d-none d-md-flex">
<li class="nav-item">
<!--
<a class="nav-link" href="/_sish/console"><i class="fa fa-home"></i> Home</a>
-->
</li>
</ul>
</div>
Expand Down
6 changes: 3 additions & 3 deletions templates/routes.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
$(function() {
$.getJSON(window.location.href.replace("console", "api/clients"), function(data) {
for (var client in data.clients) {
var clientData = data.clients[client]
var elementID = clientData.remoteAddr.replace(/(\.|:)/g, "-")
var clientData = data.clients[client];
var elementID = clientData.remoteAddr.replace(/(\.|:|\[|\])/g, "-");

$("#client-list").append(`
<tr class="client-row" id="${elementID}">
Expand All @@ -70,7 +70,7 @@
<td>${clientData.listeners.length}</td>
<td><a style="text-transform: none; color: white;" class="btn btn-danger" onclick="disconnectClient('${clientData.remoteAddr}')">Disconnect</a></td>
</tr>
`)
`);

$(`#${elementID}`).data("client-data", clientData);
}
Expand Down

0 comments on commit 9302841

Please sign in to comment.