Skip to content

Commit

Permalink
Merge pull request joewalnes#208 from asergeyev/i206
Browse files Browse the repository at this point in the history
Feature that allows to open secondary HTTP port...
  • Loading branch information
asergeyev committed May 18, 2016
2 parents 9cafe53 + 14f5d86 commit 9602821
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Config struct {
Addr []string // TCP addresses to listen on. e.g. ":1234", "1.2.3.4:1234" or "[::1]:1234"
MaxForks int // Number of allowable concurrent forks
LogLevel libwebsocketd.LogLevel
RedirPort int
CertFile, KeyFile string
*libwebsocketd.Config
}
Expand Down Expand Up @@ -71,6 +72,7 @@ func parseCommandLine() *Config {
sslCert := flag.String("sslcert", "", "Should point to certificate PEM file when --ssl is used")
sslKey := flag.String("sslkey", "", "Should point to certificate private key file when --ssl is used")
maxForksFlag := flag.Int("maxforks", 0, "Max forks, zero means unlimited")
redirPortFlag := flag.Int("redirport", 0, "HTTP port to redirect to canonical --port address")

// lib config options
binaryFlag := flag.Bool("binary", false, "Set websocketd to experimental binary mode (default is line by line)")
Expand Down Expand Up @@ -119,6 +121,7 @@ func parseCommandLine() *Config {
mainConfig.Addr = []string{fmt.Sprintf(":%d", port)}
}
mainConfig.MaxForks = *maxForksFlag
mainConfig.RedirPort = *redirPortFlag
mainConfig.LogLevel = libwebsocketd.LevelFromString(*logLevelFlag)
if mainConfig.LogLevel == libwebsocketd.LogUnknown {
fmt.Printf("Incorrect loglevel flag '%s'. Use --help to see allowed values.\n", *logLevelFlag)
Expand Down
5 changes: 5 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ Options:
--sslcert=FILE All three options must be used or all of
--sslkey=FILE them should be omitted.
--redirport=PORT Open alternative port and redirect HTTP traffic
from it to canonical address (mostly useful
for HTTPS-only configurations to redirect HTTP
traffic)
--passenv VAR[,VAR...] Lists environment variables allowed to be
passed to executed scripts.
Expand Down
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"os"
"strconv"
"strings"

"github.com/joewalnes/websocketd/libwebsocketd"
Expand Down Expand Up @@ -76,13 +77,34 @@ func main() {
// ListenAndServe is blocking function. Let's run it in
// go routine, reporting result to control channel.
// Since it's blocking it'll never return non-error.

go func(addr string) {
if config.Ssl {
rejects <- http.ListenAndServeTLS(addr, config.CertFile, config.KeyFile, nil)
} else {
rejects <- http.ListenAndServe(addr, nil)
}
}(addrSingle)

if config.RedirPort != 0 {
go func(addr string) {
pos := strings.IndexByte(addr, ':')
rediraddr := addr[:pos] + ":" + strconv.Itoa(config.RedirPort) // it would be silly to optimize this one
redir := &http.Server{Addr: rediraddr, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

// redirect to same hostname as in request but different port and probably schema
uri := "https://"
if !config.Ssl {
uri = "http://"
}
uri += r.Host[:strings.IndexByte(r.Host, ':')] + addr[pos:] + "/"

http.Redirect(w, r, uri, http.StatusMovedPermanently)
})}
log.Info("server", "Starting redirect server : http://%s/", rediraddr)
rejects <- redir.ListenAndServe()
}(addrSingle)
}
}
select {
case err := <-rejects:
Expand Down

0 comments on commit 9602821

Please sign in to comment.