This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
router: Merge pull request #4 from miracle2k/master
Add support for HTTPS routing
- Loading branch information
Showing
5 changed files
with
238 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/pem" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
"github.com/flynn/rpcplus" | ||
"github.com/flynn/strowger/types" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
client, err := rpcplus.DialHTTP("tcp", "localhost:1115") | ||
rpcAddr := flag.String("rpc", "localhost:1115", "strowger RPC address to connect to") | ||
certPath := flag.String("cert", "", "path to DER encoded certificate for SSL, - for stdin") | ||
keyPath := flag.String("key", "", "path to DER encoded private key for SSL, - for stdin") | ||
flag.Parse() | ||
if len(flag.Args()) != 2 { | ||
fmt.Fprintf(os.Stderr, "Usage: %s [flags] domain service-name\n", os.Args[0]) | ||
flag.PrintDefaults() | ||
os.Exit(64) | ||
} | ||
|
||
domain, serviceName := flag.Arg(0), flag.Arg(1) | ||
|
||
var stdin []byte | ||
var err error | ||
if *certPath == "-" || *keyPath == "-" { | ||
stdin, err = ioutil.ReadAll(os.Stdin) | ||
if err != nil { | ||
log.Fatal("Failed to read from stdin: ", err) | ||
} | ||
} | ||
|
||
tlsCert, err := readCert(*certPath, stdin) | ||
if err != nil { | ||
return | ||
} | ||
tlsKey, err := readKey(*keyPath, stdin) | ||
if err != nil { | ||
return | ||
} | ||
|
||
client, err := rpcplus.DialHTTP("tcp", *rpcAddr) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
err = client.Call("Router.AddFrontend", &strowger.Config{Service: "example-server", HTTPDomain: os.Args[1]}, &struct{}{}) | ||
frontendConfig := &strowger.Config{ | ||
Service: serviceName, | ||
HTTPDomain: domain, | ||
HTTPSCert: tlsCert, | ||
HTTPSKey: tlsKey, | ||
} | ||
err = client.Call("Router.AddFrontend", frontendConfig, &struct{}{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func readCert(path string, stdin []byte) ([]byte, error) { | ||
if path == "-" { | ||
var buffer bytes.Buffer | ||
var derBlock *pem.Block | ||
for { | ||
derBlock, stdin = pem.Decode(stdin) | ||
if derBlock == nil { | ||
break | ||
} | ||
if derBlock.Type == "CERTIFICATE" { | ||
buffer.Write(pem.EncodeToMemory(derBlock)) | ||
} | ||
} | ||
if buffer.Len() > 0 { | ||
return buffer.Bytes(), nil | ||
} | ||
log.Fatal("No certificate PEM blocks found in stdin") | ||
} | ||
return readFile(path) | ||
} | ||
|
||
func readKey(path string, stdin []byte) ([]byte, error) { | ||
if path == "-" { | ||
var derBlock *pem.Block | ||
for { | ||
derBlock, stdin = pem.Decode(stdin) | ||
if derBlock == nil { | ||
break | ||
} | ||
if strings.Contains(derBlock.Type, "PRIVATE KEY") { | ||
return pem.EncodeToMemory(derBlock), nil | ||
} | ||
} | ||
log.Fatal("No private key PEM blocks found in stdin") | ||
} | ||
return readFile(path) | ||
} | ||
|
||
func readFile(path string) ([]byte, error) { | ||
if path == "" { | ||
return nil, nil | ||
} | ||
|
||
contents, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
log.Printf("Failed to open %s: %s", path, err) | ||
return nil, err | ||
} | ||
return contents, nil | ||
} |
Oops, something went wrong.