Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
base repository: shaneHowearth/snowflake
base: master
head repository: shaneHowearth/snowflake
compare: linter-fixes
Checking mergeability… Don’t worry, you can still create the pull request.
  • 7 commits
  • 15 files changed
  • 0 comments
  • 1 contributor
Commits on Oct 03, 2019
- Error strings are no longer capitalized nor end with punctuation
- Alias import
- Remove extraneous initilisation code (No need to provide zero value
	for variables, because the compiler does that anyway)
last was initialised twice (creating a shadow), the second time inside
a case statement. The second initialisation is removed, keeping the use
of last aligned to the isame style as its use other parts of the case
statement.
@@ -183,7 +183,9 @@ func proxyPolls(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
return
}
log.Println("Passing client offer to snowflake.")
w.Write(offer)
if _, err := w.Write(offer); err != nil {
log.Printf("proxyPolls unable to write offer with error: %v", err)
}
}

/*
@@ -217,14 +219,18 @@ func clientOffers(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
case answer := <-snowflake.answerChannel:
log.Println("Client: Retrieving answer")
ctx.metrics.clientProxyMatchCount++
w.Write(answer)
if _, err := w.Write(answer); err != nil {
log.Printf("unable to write answer with error: %v", err)
}
// Initial tracking of elapsed time.
ctx.metrics.clientRoundtripEstimate = time.Since(startTime) /
time.Millisecond
case <-time.After(time.Second * ClientTimeout):
log.Println("Client: Timed out.")
w.WriteHeader(http.StatusGatewayTimeout)
w.Write([]byte("timed out waiting for answer!"))
if _, err := w.Write([]byte("timed out waiting for answer!")); err != nil {
log.Printf("unable to write timeout error, failed with error: %v", err)
}
}
}

@@ -266,12 +272,16 @@ func debugHandler(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
}
s += fmt.Sprintf("\tstandalone proxies: %d", standalones)
s += fmt.Sprintf("\n\tbrowser proxies: %d", browsers)
w.Write([]byte(s))
if _, err := w.Write([]byte(s)); err != nil {
log.Printf("writing proxy information returned error: %v ", err)
}
}

func robotsTxtHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write([]byte("User-agent: *\nDisallow: /\n"))
if _, err := w.Write([]byte("User-agent: *\nDisallow: /\n")); err != nil {
log.Printf("robotsTxtHandler unable to write, with this error: %v", err)
}
}

func metricsHandler(metricsFilename string, w http.ResponseWriter, r *http.Request) {
@@ -288,7 +298,9 @@ func metricsHandler(metricsFilename string, w http.ResponseWriter, r *http.Reque
return
}

io.Copy(w, metricsFile)
if _, err := io.Copy(w, metricsFile); err != nil {
log.Printf("copying metricsFile returned error: %v", err)
}
}

func main() {
@@ -317,7 +329,7 @@ func main() {
flag.Parse()

var err error
var metricsFile io.Writer = os.Stdout
var metricsFile io.Writer
var logOutput io.Writer = os.Stderr
//We want to send the log output through our scrubber first
log.SetOutput(&safelog.LogScrubber{Output: logOutput})
@@ -339,7 +351,7 @@ func main() {
ctx := NewBrokerContext(metricsLogger)

if !disableGeoip {
err := ctx.metrics.LoadGeoipDatabases(geoipDatabase, geoip6Database)
err = ctx.metrics.LoadGeoipDatabases(geoipDatabase, geoip6Database)
if err != nil {
log.Fatal(err.Error())
}
@@ -368,8 +380,10 @@ func main() {
go func() {
for {
signal := <-sigChan
log.Println("Received signal:", signal, ". Reloading geoip databases.")
ctx.metrics.LoadGeoipDatabases(geoipDatabase, geoip6Database)
log.Printf("Received signal: %s. Reloading geoip databases.", signal)
if err = ctx.metrics.LoadGeoipDatabases(geoipDatabase, geoip6Database); err != nil {
log.Fatalf("reload of Geo IP databases on signal %s returned error: %v", signal, err)
}
}
}()

@@ -93,7 +93,7 @@ func (table *GeoIPv6Table) Unlock() { (*table).lock.Unlock() }
func geoipStringToIP(ipStr string) (net.IP, error) {
ip, err := strconv.ParseUint(ipStr, 10, 32)
if err != nil {
return net.IPv4(0, 0, 0, 0), fmt.Errorf("Error parsing IP %s", ipStr)
return net.IPv4(0, 0, 0, 0), fmt.Errorf("error parsing IP %s", ipStr)
}
var bytes [4]byte
bytes[0] = byte(ip & 0xFF)
@@ -115,7 +115,7 @@ func (table *GeoIPv4Table) parseEntry(candidate string) (*GeoIPEntry, error) {
parsedCandidate := strings.Split(candidate, ",")

if len(parsedCandidate) != 3 {
return nil, fmt.Errorf("Provided geoip file is incorrectly formatted. Could not parse line:\n%s", parsedCandidate)
return nil, fmt.Errorf("provided geoip file is incorrectly formatted. Could not parse line:\n%s", parsedCandidate)
}

low, err := geoipStringToIP(parsedCandidate[0])
@@ -190,7 +190,7 @@ func GeoIPLoadFile(table GeoIPTable, pathname string) error {
for scanner.Scan() {
entry, err := table.parseEntry(scanner.Text())
if err != nil {
return fmt.Errorf("Provided geoip file is incorrectly formatted. Line is: %+q", scanner.Text())
return fmt.Errorf("provided geoip file is incorrectly formatted. Line is: %+q", scanner.Text())
}

if entry != nil {
@@ -121,7 +121,6 @@ func (m *Metrics) UpdateCountryStats(addr string) {
m.countryStats.counts[country]++
m.countryStats.addrs[addr] = true

return
}

func (m *Metrics) LoadGeoipDatabases(geoipDB string, geoip6DB string) error {
@@ -133,19 +132,16 @@ func (m *Metrics) LoadGeoipDatabases(geoipDB string, geoip6DB string) error {
if err != nil {
m.tablev4 = nil
return err
} else {
m.tablev4 = tablev4
}
m.tablev4 = tablev4

tablev6 := new(GeoIPv6Table)
err = GeoIPLoadFile(tablev6, geoip6DB)
if err != nil {
m.tablev6 = nil
return err
} else {
m.tablev6 = tablev6
}

m.tablev6 = tablev6
return nil
}

@@ -3,7 +3,6 @@ package main
import (
"bytes"
"container/heap"
. "github.com/smartystreets/goconvey/convey"
"io/ioutil"
"log"
"net"
@@ -12,6 +11,8 @@ import (
"os"
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"
)

func NullLogger() *log.Logger {
@@ -181,7 +182,7 @@ func TestBroker(t *testing.T) {
})

Convey("with error if the proxy writes too much data", func() {
data := bytes.NewReader(make([]byte, 100001, 100001))
data := bytes.NewReader(make([]byte, 100001))
r, err := http.NewRequest("POST", "snowflake.broker/answer", data)
r.Header.Set("X-Session-ID", "test")
So(err, ShouldBeNil)
@@ -385,7 +386,9 @@ func TestGeoip(t *testing.T) {

// Make sure things behave properly if geoip file fails to load
ctx := NewBrokerContext(NullLogger())
ctx.metrics.LoadGeoipDatabases("invalid_filename", "invalid_filename6")
if err := ctx.metrics.LoadGeoipDatabases("invalid_filename", "invalid_filename6"); err != nil {
log.Printf("loading geo ip databases returned error: %v", err)
}
ctx.metrics.UpdateCountryStats("127.0.0.1")
So(ctx.metrics.tablev4, ShouldEqual, nil)

@@ -504,6 +507,9 @@ func TestMetrics(t *testing.T) {

data = bytes.NewReader([]byte("test"))
r, err = http.NewRequest("POST", "snowflake.broker/proxy", data)
if err != nil {
log.Printf("unable to get NewRequest with error: %v", err)
}
r.Header.Set("X-Session-ID", "test")
r.RemoteAddr = "129.97.208.23:8888" //CA geoip
go func(ctx *BrokerContext) {
@@ -44,13 +44,13 @@ func (p *Peers) Collect() (Snowflake, error) {
cnt := p.Count()
s := fmt.Sprintf("Currently at [%d/%d]", cnt, p.capacity)
if cnt >= p.capacity {
s := fmt.Sprintf("At capacity [%d/%d]", cnt, p.capacity)
s = fmt.Sprintf("At capacity [%d/%d]", cnt, p.capacity)
return nil, errors.New(s)
}
log.Println("WebRTC: Collecting a new Snowflake.", s)
// Engage the Snowflake Catching interface, which must be available.
if nil == p.Tongue {
return nil, errors.New("Missing Tongue to catch Snowflakes with.")
return nil, errors.New("missing Tongue to catch Snowflakes with")
}
// BUG: some broker conflict here.
connection, err := p.Tongue.Catch()
@@ -141,7 +141,7 @@ func NewWebRTCDialer(
// Initialize a WebRTC Connection by signaling through the broker.
func (w WebRTCDialer) Catch() (Snowflake, error) {
if nil == w.BrokerChannel {
return nil, errors.New("Cannot Dial WebRTC without a BrokerChannel.")
return nil, errors.New("cannot Dial WebRTC without a BrokerChannel")
}
// TODO: [#3] Fetch ICE server information from Broker.
// TODO: [#18] Consider TURN servers here too.
@@ -13,7 +13,7 @@ const (
SnowflakeTimeout = 30
)

// When a connection handler starts, +1 is written to this channel; when it
// HandlerChan - When a connection handler starts, +1 is written to this channel; when it
// ends, -1 is written.
var HandlerChan = make(chan int)

@@ -27,7 +27,10 @@ func Handler(socks SocksConnector, snowflakes SnowflakeCollector) error {
// Obtain an available WebRTC remote. May block.
snowflake := snowflakes.Pop()
if nil == snowflake {
socks.Reject()
if err := socks.Reject(); err != nil {
log.Printf("socks.Reject returned error: %v", err)
}

return errors.New("handler: Received invalid Snowflake")
}
defer socks.Close()
@@ -53,15 +56,19 @@ func Handler(socks SocksConnector, snowflakes SnowflakeCollector) error {

// Exchanges bytes between two ReadWriters.
// (In this case, between a SOCKS and WebRTC connection.)
func copyLoop(a, b io.ReadWriter) {
func copyLoop(WebRTC, SOCKS io.ReadWriter) {
var wg sync.WaitGroup
wg.Add(2)
go func() {
io.Copy(b, a)
if _, err := io.Copy(SOCKS, WebRTC); err != nil {
log.Printf("copying WebRTC to SOCKS resulted in error: %v", err)
}
wg.Done()
}()
go func() {
io.Copy(a, b)
if _, err := io.Copy(WebRTC, SOCKS); err != nil {
log.Printf("copying SOCKS to WebRTC resulted in error: %v", err)
}
wg.Done()
}()
wg.Wait()
@@ -60,7 +60,6 @@ func (b *BytesSyncLogger) Log() {
case amount = <-b.OutboundChan:
b.Outbound += amount
b.OutEvents++
last := time.Now()
if time.Since(last) > time.Second*LogTimeInterval {
last = time.Now()
output()
@@ -138,7 +138,8 @@ func (c *WebRTCPeer) Connect() error {
}
err = c.establishDataChannel()
if err != nil {
return errors.New("WebRTC: Could not establish DataChannel.")
// nolint: golint
return errors.New("WebRTC: Could not establish DataChannel")
}
err = c.exchangeSDP()
if err != nil {
@@ -151,7 +152,9 @@ func (c *WebRTCPeer) Connect() error {
// Create and prepare callbacks on a new WebRTC PeerConnection.
func (c *WebRTCPeer) preparePeerConnection() error {
if nil != c.pc {
c.pc.Destroy()
if err := c.pc.Destroy(); err != nil {
log.Printf("c.pc.Destroy returned error: %v", err)
}
c.pc = nil
}
pc, err := webrtc.NewPeerConnection(c.config)
@@ -256,7 +259,9 @@ func (c *WebRTCPeer) establishDataChannel() error {
if err != nil {
// TODO: Maybe shouldn't actually close.
log.Println("Error writing to SOCKS pipe")
c.writePipe.CloseWithError(err)
if inerr := c.writePipe.CloseWithError(err); inerr != nil {
log.Printf("c.writePipe.CloseWithError returned error: %v", inerr)
}
}
if n != len(msg) {
log.Println("Error: short write")
@@ -293,7 +298,7 @@ func (c *WebRTCPeer) exchangeSDP() error {
}
// Keep trying the same offer until a valid answer arrives.
var ok bool
var answer *webrtc.SessionDescription = nil
var answer *webrtc.SessionDescription
for nil == answer {
go c.sendOfferToBroker()
answer, ok = <-c.answerChannel // Blocks...

No commit comments for this range