本文整理汇总了Golang中github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.ContextError函数的典型用法代码示例。如果您正苦于以下问题:Golang ContextError函数的具体用法?Golang ContextError怎么用?Golang ContextError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ContextError函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: connectedAPIRequestHandler
// connectedAPIRequestHandler implements the "connected" API request.
// Clients make the connected request once a tunnel connection has been
// established and at least once per day. The last_connected input value,
// which should be a connected_timestamp output from a previous connected
// response, is used to calculate unique user stats.
func connectedAPIRequestHandler(
support *SupportServices,
geoIPData GeoIPData,
params requestJSONObject) ([]byte, error) {
err := validateRequestParams(support, params, connectedRequestParams)
if err != nil {
return nil, common.ContextError(err)
}
log.LogRawFieldsWithTimestamp(
getRequestLogFields(
support,
"connected",
geoIPData,
params,
connectedRequestParams))
connectedResponse := common.ConnectedResponse{
ConnectedTimestamp: common.TruncateTimestampToHour(common.GetCurrentTimestamp()),
}
responsePayload, err := json.Marshal(connectedResponse)
if err != nil {
return nil, common.ContextError(err)
}
return responsePayload, nil
}
示例2: tunneledLookupIP
// tunneledLookupIP resolves a split tunnel candidate hostname with a tunneled
// DNS request.
func tunneledLookupIP(
dnsServerAddress string, dnsTunneler Tunneler, host string) (addr net.IP, ttl time.Duration, err error) {
ipAddr := net.ParseIP(host)
if ipAddr != nil {
// maxDuration from golang.org/src/time/time.go
return ipAddr, time.Duration(1<<63 - 1), nil
}
// dnsServerAddress must be an IP address
ipAddr = net.ParseIP(dnsServerAddress)
if ipAddr == nil {
return nil, 0, common.ContextError(errors.New("invalid IP address"))
}
// Dial's alwaysTunnel is set to true to ensure this connection
// is tunneled (also ensures this code path isn't circular).
// Assumes tunnel dialer conn configures timeouts and interruptibility.
conn, err := dnsTunneler.Dial(fmt.Sprintf(
"%s:%d", dnsServerAddress, DNS_PORT), true, nil)
if err != nil {
return nil, 0, common.ContextError(err)
}
ipAddrs, ttls, err := ResolveIP(host, conn)
if err != nil {
return nil, 0, common.ContextError(err)
}
if len(ipAddrs) < 1 {
return nil, 0, common.ContextError(errors.New("no IP address"))
}
return ipAddrs[0], ttls[0], nil
}
示例3: encryptAESCBC
// Encrypt plaintext with AES in CBC mode.
func encryptAESCBC(plaintext []byte) ([]byte, []byte, []byte, error) {
// CBC mode works on blocks so plaintexts need to be padded to the
// next whole block (https://tools.ietf.org/html/rfc5246#section-6.2.3.2).
plaintext = AddPKCS7Padding(plaintext, aes.BlockSize)
ciphertext := make([]byte, len(plaintext))
iv, err := common.MakeSecureRandomBytes(aes.BlockSize)
if err != nil {
return nil, nil, nil, err
}
key, err := common.MakeSecureRandomBytes(aes.BlockSize)
if err != nil {
return nil, nil, nil, common.ContextError(err)
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, nil, nil, common.ContextError(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, plaintext)
return iv, key, ciphertext, nil
}
示例4: NewServerContext
// NewServerContext makes the tunnelled handshake request to the Psiphon server
// and returns a ServerContext struct for use with subsequent Psiphon server API
// requests (e.g., periodic connected and status requests).
func NewServerContext(tunnel *Tunnel, sessionId string) (*ServerContext, error) {
// For legacy servers, set up psiphonHttpsClient for
// accessing the Psiphon API via the web service.
var psiphonHttpsClient *http.Client
if !tunnel.serverEntry.SupportsSSHAPIRequests() ||
tunnel.config.TargetApiProtocol == common.PSIPHON_WEB_API_PROTOCOL {
var err error
psiphonHttpsClient, err = makePsiphonHttpsClient(tunnel)
if err != nil {
return nil, common.ContextError(err)
}
}
serverContext := &ServerContext{
sessionId: sessionId,
tunnelNumber: atomic.AddInt64(&nextTunnelNumber, 1),
tunnel: tunnel,
psiphonHttpsClient: psiphonHttpsClient,
}
err := serverContext.doHandshakeRequest()
if err != nil {
return nil, common.ContextError(err)
}
return serverContext, nil
}
示例5: HandleOSLRequest
func HandleOSLRequest(
tunnelOwner TunnelOwner, tunnel *Tunnel, payload []byte) error {
var oslRequest protocol.OSLRequest
err := json.Unmarshal(payload, &oslRequest)
if err != nil {
return common.ContextError(err)
}
if oslRequest.ClearLocalSLOKs {
DeleteSLOKs()
}
seededNewSLOK := false
for _, slok := range oslRequest.SeedPayload.SLOKs {
duplicate, err := SetSLOK(slok.ID, slok.Key)
if err != nil {
// TODO: return error to trigger retry?
NoticeAlert("SetSLOK failed: %s", common.ContextError(err))
} else if !duplicate {
seededNewSLOK = true
}
if tunnel.config.EmitSLOKs {
NoticeSLOKSeeded(base64.StdEncoding.EncodeToString(slok.ID), duplicate)
}
}
if seededNewSLOK {
tunnelOwner.SignalSeededNewSLOK()
}
return nil
}
示例6: parseResolveConf
func parseResolveConf(filename string) (net.IP, error) {
file, err := os.Open(filename)
if err != nil {
return nil, common.ContextError(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, ";") || strings.HasPrefix(line, "#") {
continue
}
fields := strings.Fields(line)
if len(fields) == 2 && fields[0] == "nameserver" {
// TODO: parseResolverAddress will fail when the nameserver
// is not an IP address. It may be a domain name. To support
// this case, should proceed to the next "nameserver" line.
return parseResolver(fields[1])
}
}
if err := scanner.Err(); err != nil {
return nil, common.ContextError(err)
}
return nil, common.ContextError(errors.New("nameserver not found"))
}
示例7: validateRequestParams
func validateRequestParams(
support *SupportServices,
params requestJSONObject,
expectedParams []requestParamSpec) error {
for _, expectedParam := range expectedParams {
value := params[expectedParam.name]
if value == nil {
if expectedParam.flags&requestParamOptional != 0 {
continue
}
return common.ContextError(
fmt.Errorf("missing param: %s", expectedParam.name))
}
var err error
if expectedParam.flags&requestParamArray != 0 {
err = validateStringArrayRequestParam(support, expectedParam, value)
} else {
err = validateStringRequestParam(support, expectedParam, value)
}
if err != nil {
return common.ContextError(err)
}
}
return nil
}
示例8: NewSupportServices
// NewSupportServices initializes a new SupportServices.
func NewSupportServices(config *Config) (*SupportServices, error) {
trafficRulesSet, err := NewTrafficRulesSet(config.TrafficRulesFilename)
if err != nil {
return nil, common.ContextError(err)
}
psinetDatabase, err := psinet.NewDatabase(config.PsinetDatabaseFilename)
if err != nil {
return nil, common.ContextError(err)
}
geoIPService, err := NewGeoIPService(
config.GeoIPDatabaseFilenames, config.DiscoveryValueHMACKey)
if err != nil {
return nil, common.ContextError(err)
}
dnsResolver, err := NewDNSResolver(config.DNSResolverIPAddress)
if err != nil {
return nil, common.ContextError(err)
}
return &SupportServices{
Config: config,
TrafficRulesSet: trafficRulesSet,
PsinetDatabase: psinetDatabase,
GeoIPService: geoIPService,
DNSResolver: dnsResolver,
}, nil
}
示例9: unpackRemoteServerListFile
// unpackRemoteServerListFile reads a file that contains a
// zlib compressed authenticated data package, validates
// the package, and returns the payload.
func unpackRemoteServerListFile(
config *Config, filename string) (string, error) {
fileReader, err := os.Open(filename)
if err != nil {
return "", common.ContextError(err)
}
defer fileReader.Close()
zlibReader, err := zlib.NewReader(fileReader)
if err != nil {
return "", common.ContextError(err)
}
dataPackage, err := ioutil.ReadAll(zlibReader)
zlibReader.Close()
if err != nil {
return "", common.ContextError(err)
}
payload, err := common.ReadAuthenticatedDataPackage(
dataPackage, config.RemoteServerListSignaturePublicKey)
if err != nil {
return "", common.ContextError(err)
}
return payload, nil
}
示例10: scanServerEntries
func scanServerEntries(scanner func(*protocol.ServerEntry)) error {
err := singleton.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(serverEntriesBucket))
cursor := bucket.Cursor()
for key, value := cursor.First(); key != nil; key, value = cursor.Next() {
serverEntry := new(protocol.ServerEntry)
err := json.Unmarshal(value, serverEntry)
if err != nil {
// In case of data corruption or a bug causing this condition,
// do not stop iterating.
NoticeAlert("scanServerEntries: %s", common.ContextError(err))
continue
}
scanner(serverEntry)
}
return nil
})
if err != nil {
return common.ContextError(err)
}
return nil
}
示例11: NewDatabase
// NewDatabase initializes a Database, calling Reload on the specified
// filename.
func NewDatabase(filename string) (*Database, error) {
database := &Database{}
database.ReloadableFile = common.NewReloadableFile(
filename,
func(filename string) error {
psinetJSON, err := ioutil.ReadFile(filename)
if err != nil {
// On error, state remains the same
return common.ContextError(err)
}
err = json.Unmarshal(psinetJSON, &database)
if err != nil {
// On error, state remains the same
// (Unmarshal first validates the provided
// JOSN and then populates the interface)
return common.ContextError(err)
}
return nil
})
_, err := database.Reload()
if err != nil {
return nil, common.ContextError(err)
}
return database, nil
}
示例12: InitLogging
// InitLogging configures a logger according to the specified
// config params. If not called, the default logger set by the
// package init() is used.
// Concurrenty note: should only be called from the main
// goroutine.
func InitLogging(config *Config) error {
level, err := logrus.ParseLevel(config.LogLevel)
if err != nil {
return common.ContextError(err)
}
logWriter := os.Stderr
if config.LogFilename != "" {
logWriter, err = os.OpenFile(
config.LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return common.ContextError(err)
}
}
log = &ContextLogger{
&logrus.Logger{
Out: logWriter,
Formatter: &CustomJSONFormatter{},
Level: level,
},
}
return nil
}
示例13: readSshPacket
func readSshPacket(
conn net.Conn, deobfuscate func([]byte)) ([]byte, bool, error) {
prefix := make([]byte, SSH_PACKET_PREFIX_LENGTH)
_, err := io.ReadFull(conn, prefix)
if err != nil {
return nil, false, common.ContextError(err)
}
deobfuscate(prefix)
packetLength, _, payloadLength, messageLength := getSshPacketPrefix(prefix)
if packetLength > SSH_MAX_PACKET_LENGTH {
return nil, false, common.ContextError(errors.New("ssh packet length too large"))
}
readBuffer := make([]byte, messageLength)
copy(readBuffer, prefix)
_, err = io.ReadFull(conn, readBuffer[len(prefix):])
if err != nil {
return nil, false, common.ContextError(err)
}
deobfuscate(readBuffer[len(prefix):])
isMsgNewKeys := false
if payloadLength > 0 {
packetType := int(readBuffer[SSH_PACKET_PREFIX_LENGTH])
if packetType == SSH_MSG_NEWKEYS {
isMsgNewKeys = true
}
}
return readBuffer, isMsgNewKeys, nil
}
示例14: NewClientObfuscator
// NewClientObfuscator creates a new Obfuscator, staging a seed message to be
// sent to the server (by the caller) and initializing stream ciphers to
// obfuscate data.
func NewClientObfuscator(
config *ObfuscatorConfig) (obfuscator *Obfuscator, err error) {
seed, err := common.MakeSecureRandomBytes(OBFUSCATE_SEED_LENGTH)
if err != nil {
return nil, common.ContextError(err)
}
clientToServerCipher, serverToClientCipher, err := initObfuscatorCiphers(seed, config)
if err != nil {
return nil, common.ContextError(err)
}
maxPadding := OBFUSCATE_MAX_PADDING
if config.MaxPadding > 0 {
maxPadding = config.MaxPadding
}
seedMessage, err := makeSeedMessage(maxPadding, seed, clientToServerCipher)
if err != nil {
return nil, common.ContextError(err)
}
return &Obfuscator{
seedMessage: seedMessage,
clientToServerCipher: clientToServerCipher,
serverToClientCipher: serverToClientCipher}, nil
}
示例15: NewTrafficRulesSet
// NewTrafficRulesSet initializes a TrafficRulesSet with
// the rules data in the specified config file.
func NewTrafficRulesSet(filename string) (*TrafficRulesSet, error) {
set := &TrafficRulesSet{}
set.ReloadableFile = common.NewReloadableFile(
filename,
func(filename string) error {
configJSON, err := ioutil.ReadFile(filename)
if err != nil {
// On error, state remains the same
return common.ContextError(err)
}
var newSet TrafficRulesSet
err = json.Unmarshal(configJSON, &newSet)
if err != nil {
return common.ContextError(err)
}
err = newSet.Validate()
if err != nil {
return common.ContextError(err)
}
// Modify actual traffic rules only after validation
set.DefaultRules = newSet.DefaultRules
set.FilteredRules = newSet.FilteredRules
return nil
})
_, err := set.Reload()
if err != nil {
return nil, common.ContextError(err)
}
return set, nil
}