Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
router: Merge pull request #4 from miracle2k/master
Browse files Browse the repository at this point in the history
Add support for HTTPS routing
  • Loading branch information
titanous committed Mar 14, 2014
2 parents d59ba8f + 9f7b038 commit 4c9e2ed
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 33 deletions.
6 changes: 5 additions & 1 deletion Godeps
Expand Up @@ -18,6 +18,10 @@
{
"ImportPath": "github.com/flynn/rpcplus",
"Rev": "68355e62cadb10d431f81b507ee30805b99022cd"
}
},
{
"ImportPath": "github.com/inconshreveable/go-vhost",
"Rev": "abc5f6a77596abba0c71c07cfe22b6ccfd0a7d2d"
}
]
}
97 changes: 95 additions & 2 deletions examples/client/client.go
@@ -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
}

0 comments on commit 4c9e2ed

Please sign in to comment.