github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/tor.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package libkb
     5  
     6  import (
     7  	"fmt"
     8  	"net/url"
     9  )
    10  
    11  type TorMode int
    12  
    13  const (
    14  	TorNone TorMode = iota
    15  	TorStrict
    16  	TorLeaky
    17  )
    18  
    19  func (m TorMode) Enabled() bool {
    20  	return (m != TorNone)
    21  }
    22  
    23  func (m TorMode) UseCookies() bool {
    24  	return m != TorStrict
    25  }
    26  
    27  func (m TorMode) UseSession() bool {
    28  	return m != TorStrict
    29  }
    30  
    31  func (m TorMode) UseCSRF() bool {
    32  	return m != TorStrict
    33  }
    34  
    35  func (m TorMode) UseHeaders() bool {
    36  	return m != TorStrict
    37  }
    38  
    39  func StringToTorMode(s string) (ret TorMode, err error) {
    40  	switch s {
    41  	case "strict":
    42  		ret = TorStrict
    43  	case "leaky":
    44  		ret = TorLeaky
    45  	case "none":
    46  		ret = TorNone
    47  	default:
    48  		err = fmt.Errorf("Unknown Tor mode: '%s'", s)
    49  	}
    50  	return ret, err
    51  }
    52  
    53  func TorParseProxy(s string) (*url.URL, error) {
    54  	if s == "" {
    55  		return nil, nil
    56  	}
    57  	return url.Parse(s)
    58  }