From 3a24b8b309db15850e9f87941777d8792c2d62f9 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 29 Jun 2017 02:04:19 +0200 Subject: added config parser for new config options --- src/daq/s5proxy/sample.json | 4 +- src/daq/s5proxy/src/s5proxy/config.go | 78 +++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) (limited to 'src/daq/s5proxy') diff --git a/src/daq/s5proxy/sample.json b/src/daq/s5proxy/sample.json index d45a4d5..3b26879 100644 --- a/src/daq/s5proxy/sample.json +++ b/src/daq/s5proxy/sample.json @@ -1,8 +1,10 @@ { "listen": ":8443", - "connect": "http://emc01.spreadspace.org:8000", + "protocol": "http+https", + "redirect2https": 301, "cert": "fullchain.pem", "key": "private.key", + "connect": "http://emc01.spreadspace.org:8000", "request_header": [ { "op": "del", "header": "X-Forwarded-For" } ], diff --git a/src/daq/s5proxy/src/s5proxy/config.go b/src/daq/s5proxy/src/s5proxy/config.go index 1fc721d..62ea047 100644 --- a/src/daq/s5proxy/src/s5proxy/config.go +++ b/src/daq/s5proxy/src/s5proxy/config.go @@ -35,10 +35,86 @@ package main import ( "encoding/json" "errors" + "fmt" + "net/http" "os" + "strconv" + "strings" "time" ) +type ProtocolType string + +const ( + HTTPAndHTTPS ProtocolType = "http+https" + HTTPOnly ProtocolType = "http" + HTTPSOnly ProtocolType = "https" +) + +func (p ProtocolType) String() string { + return string(p) +} + +func (p *ProtocolType) fromString(str string) (err error) { + switch strings.ToLower(str) { + case string(HTTPAndHTTPS): + fallthrough + case string(HTTPOnly): + fallthrough + case string(HTTPSOnly): + *p = ProtocolType(strings.ToLower(str)) + default: + return errors.New("invalid protocol: '" + str + "'") + } + return +} + +func (p ProtocolType) MarshalText() (data []byte, err error) { + data = []byte(p.String()) + return +} + +func (p *ProtocolType) UnmarshalText(data []byte) (err error) { + return p.fromString(string(data)) +} + +type RedirectCode int + +func (r RedirectCode) String() string { + return strconv.Itoa(int(r)) +} + +func (r *RedirectCode) fromString(str string) error { + code, err := strconv.Atoi(str) + if err != nil { + return err + } + switch code { + case 0: + fallthrough + case http.StatusMovedPermanently: + fallthrough + case http.StatusFound: + fallthrough + case http.StatusSeeOther: + fallthrough + case http.StatusTemporaryRedirect: + *r = RedirectCode(code) + return nil + } + return fmt.Errorf("invalid redirect code: '"+str+"', must be one of %d, %d, %d or %d", + http.StatusMovedPermanently, http.StatusFound, http.StatusSeeOther, http.StatusTemporaryRedirect) +} + +func (r RedirectCode) MarshalJSON() (data []byte, err error) { + data = []byte(r.String()) + return +} + +func (r *RedirectCode) UnmarshalJSON(data []byte) (err error) { + return r.fromString(string(data)) +} + type Operation uint const ( @@ -118,6 +194,8 @@ type SFiveConf struct { type Config struct { ListenAddr string `json:"listen"` + Protocol ProtocolType `json:"protocol"` + Redirect2HTTPS RedirectCode `json:"redirect2https"` ConnectAddr string `json:"connect"` CertFile string `json:"cert"` KeyFile string `json:"key"` -- cgit v1.2.3