Skip to content

Commit

Permalink
uniformize fallback access data format
Browse files Browse the repository at this point in the history
  • Loading branch information
aranhoide committed Aug 12, 2015
1 parent 00ee12f commit afa10f9
Show file tree
Hide file tree
Showing 5 changed files with 530 additions and 158 deletions.
46 changes: 14 additions & 32 deletions src/github.com/getlantern/checkfallbacks/checkfallbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ var (
log = golog.LoggerFor("checkfallbacks")
)

type FallbackServer struct {
Protocol string
IP string
Port string
Pt bool
Cert string
Auth_token string
}

func main() {
flag.Parse()

Expand All @@ -61,7 +52,7 @@ func main() {

// Load the fallback servers list file. Failure to do so will result in
// exiting the program.
func loadFallbacks(filename string) (fallbacks []FallbackServer) {
func loadFallbacks(filename string) (fallbacks []client.ChainedServerInfo) {
if filename == "" {
log.Error("Please specify a fallbacks file")
flag.Usage()
Expand All @@ -86,12 +77,12 @@ func loadFallbacks(filename string) (fallbacks []FallbackServer) {
}

// Test all fallback servers
func testAllFallbacks(fallbacks []FallbackServer) (errors *chan error) {
func testAllFallbacks(fallbacks []client.ChainedServerInfo) (errors *chan error) {
errorsChan := make(chan error)
errors = &errorsChan

// Make
fbChan := make(chan FallbackServer)
fbChan := make(chan client.ChainedServerInfo)
// Channel fallback servers on-demand
go func() {
for _, val := range fallbacks {
Expand All @@ -112,7 +103,7 @@ func testAllFallbacks(fallbacks []FallbackServer) (errors *chan error) {
// Done() when closed (i.e. range exits)
go func(i int) {
for fb := range fbChan {
*errors <- fb.testFallbackServer(i)
*errors <- testFallbackServer(&fb, i)
}
workersWg.Done()
}(i + 1)
Expand All @@ -125,22 +116,13 @@ func testAllFallbacks(fallbacks []FallbackServer) (errors *chan error) {
return
}

// Perform the test of an individual FallbackServer
func (fb *FallbackServer) testFallbackServer(workerId int) (err error) {
if fb.Pt {
return fmt.Errorf("Skipping fallback %v because it has pluggable transport enabled", fb.IP)
}

// Perform the test of an individual server
func testFallbackServer(fb *client.ChainedServerInfo, workerId int) (err error) {
// Test connectivity
info := &client.ChainedServerInfo{
Addr: fb.IP + ":443",
Cert: fb.Cert,
AuthToken: fb.Auth_token,
Pipelined: true,
}
dialer, err := info.Dialer()
fb.Pipelined = true
dialer, err := fb.Dialer()
if err != nil {
return fmt.Errorf("%v: error building dialer: %v", fb.IP, err)
return fmt.Errorf("%v: error building dialer: %v", fb.Addr, err)
}
c := &http.Client{
Transport: &http.Transport{
Expand All @@ -150,21 +132,21 @@ func (fb *FallbackServer) testFallbackServer(workerId int) (err error) {
req, err := http.NewRequest("GET", "http://www.google.com/humans.txt", nil)
resp, err := c.Do(req)
if err != nil {
return fmt.Errorf("%v: requesting humans.txt failed: %v", fb.IP, err)
return fmt.Errorf("%v: requesting humans.txt failed: %v", fb.Addr, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("%v: bad status code: %v", fb.IP, resp.StatusCode)
return fmt.Errorf("%v: bad status code: %v", fb.Addr, resp.StatusCode)
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("%v: error reading response body: %v", fb.IP, err)
return fmt.Errorf("%v: error reading response body: %v", fb.Addr, err)
}
body := string(bytes)
if body != expectedBody {
return fmt.Errorf("%v: wrong body: %s", fb.IP, body)
return fmt.Errorf("%v: wrong body: %s", fb.Addr, body)
}

log.Debugf("Worker %d: Fallback %v OK.\n", workerId, fb.IP)
log.Debugf("Worker %d: Fallback %v OK.\n", workerId, fb.Addr)
return nil
}
21 changes: 8 additions & 13 deletions src/github.com/getlantern/checkfallbacks/checkfallbacks_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package main

import (
"github.com/getlantern/flashlight/client"
"reflect"
"testing"
)

func TestJSONloading(t *testing.T) {
fallbacks := loadFallbacks("test.json")

expectedFb := []FallbackServer{
expectedFb := []client.ChainedServerInfo{
{
Protocol: "tcp",
IP: "78.62.239.134",
Port: "443",
Pt: false,
Cert: "-----CERTIFICATE-----\n",
Auth_token: "a1",
Addr: "78.62.239.134:443",
Cert: "-----CERTIFICATE-----\n",
AuthToken: "a1",
},
{
Protocol: "udp",
IP: "178.62.239.34",
Port: "80",
Pt: false,
Cert: "-----CERTIFICATE-----\n",
Auth_token: "a2",
Addr: "178.62.239.34:80",
Cert: "-----CERTIFICATE-----\n",
AuthToken: "a2",
},
}

Expand Down
4 changes: 2 additions & 2 deletions src/github.com/getlantern/checkfallbacks/test.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
{"ip": "78.62.239.134", "auth_token": "a1", "cert": "-----CERTIFICATE-----\n", "protocol": "tcp", "port": "443"},
{"ip": "178.62.239.34", "auth_token": "a2", "cert": "-----CERTIFICATE-----\n", "protocol": "udp", "port": "80"}
{"addr": "78.62.239.134:443", "authtoken": "a1", "cert": "-----CERTIFICATE-----\n"},
{"addr": "178.62.239.34:80", "authtoken": "a2", "cert": "-----CERTIFICATE-----\n"}
]
Loading

0 comments on commit afa10f9

Please sign in to comment.