diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/daq/s5proxy/sample.json | 4 | ||||
-rw-r--r-- | src/daq/s5proxy/src/s5proxy/config.go | 78 |
2 files changed, 81 insertions, 1 deletions
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"` |