// Dial connects to the Redis server at the given network and address. func Dial(cx appengine.Context, network, address string) (Conn, error) { // lv3 AppEngine c, err := socket.Dial(cx, network, address) if err != nil { return nil, err } return NewConn(c, 0, 0), nil }
// DialTimeout acts like Dial but takes timeouts for establishing the // connection to the server, writing a command and reading a reply. lv3 AppEngine func DialTimeout(cx appengine.Context, network, address string, connectTimeout, readTimeout, writeTimeout time.Duration) (Conn, error) { var c *socket.Conn var err error if connectTimeout > 0 { c, err = socket.DialTimeout(cx, network, address, connectTimeout) } else { c, err = socket.Dial(cx, network, address) } if err != nil { return nil, err } return NewConn(c, readTimeout, writeTimeout), nil }
// Create a new session. Please note that you have to login to retrieve a // valid session token. func NewSession(c *Config, context appengine.Context) (*Session, error) { s := new(Session) // Configuration if c.Username == "" { return s, errors.New("Config.Username is empty.") } if c.Password == "" { return s, errors.New("Config.Password is empty.") } if _, err := os.Stat(c.CertFile); os.IsNotExist(err) { return s, errors.New("Config.CertFile does not exist.") } if _, err := os.Stat(c.KeyFile); os.IsNotExist(err) { return s, errors.New("Config.KeyFile does not exist.") } c.Exchange = strings.ToUpper(c.Exchange) if _, exists := endpointMap[c.Exchange]; exists == false { c.Exchange = "UK" } if c.Locale == "" { c.Locale = "en" } s.config = c // HTTP client cert, err := tls.LoadX509KeyPair(s.config.CertFile, s.config.KeyFile) if err != nil { return s, err } ssl := &tls.Config{ Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true, } ssl.Rand = rand.Reader s.httpClient = &http.Client{ Transport: &http.Transport{ Dial: func(network, addr string) (net.Conn, error) { return socket.Dial(context, network, addr) }, TLSClientConfig: ssl, }, } return s, nil }
func (i *Instance) httpClient(c appengine.Context) (client *http.Client, err error) { // Create TLS settings for expected TLS certificate. // We rely on knowing the cert we expect in advance, // rather than a usual certificate chain. cpool := x509.NewCertPool() if !cpool.AppendCertsFromPEM(i.Certificate) { return nil, errors.New("Could not add certificate to pool.") } tlsConfig := &tls.Config{ RootCAs: cpool, ServerName: "dreamserver", } // Create a HTTP client which uses the sockets API rather // than the urlfetch API, since we need custom TLS config, // in order to be secure. transport := &http.Transport{ TLSClientConfig: tlsConfig, Dial: func(network, addr string) (net.Conn, error) { conn, err := socket.Dial(c, network, addr) if err != nil { return nil, err } // If we don't do this step, // sockets time out after five seconds. // This is not conductive to our longer requests. err = conn.SetReadDeadline(time.Now().Add(50 * time.Minute)) if err != nil { return nil, err } err = conn.SetWriteDeadline(time.Now().Add(50 * time.Minute)) if err != nil { return nil, err } return conn, nil }, } client = &http.Client{ Transport: transport, } return }
func doRequestGet(c appengine.Context, s *Session, key, method string, body *strings.Reader) ([]byte, error) { endpoint, err := s.getUrl(key, method) if err != nil { return nil, err } req, err := http.NewRequest("GET", endpoint, body) if err != nil { return nil, err } req.Header.Set("Accept", "application/json") if key == "certLogin" || method == "login" { req.Header.Set("Content-Type", "application/x-www-form-urlencoded") // In non-interactive login, X-Application is not validated req.Header.Set("X-Application", s.config.ApplicationKey) } else { req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Authentication", s.token) if key == "account" && method == "getDeveloperAppKeys" { req.Header.Del("X-Application") } else { if s.Live { req.Header.Set("X-Application", s.appKeys[LIVE_DATA]) } else { req.Header.Set("X-Application", s.appKeys[DELAY_DATA]) } } } cert, err := tls.LoadX509KeyPair(s.config.CertFile, s.config.KeyFile) if err != nil { return nil, err } ssl := &tls.Config{ Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true, } ssl.Rand = rand.Reader s.httpClient = &http.Client{ Transport: &http.Transport{ Dial: func(network, addr string) (net.Conn, error) { return socket.Dial(c, network, addr) }, TLSClientConfig: ssl, }, } res, err := s.httpClient.Do(req) if err != nil { return nil, err } if res.StatusCode != 200 { return nil, errors.New(res.Status) } defer res.Body.Close() data, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err } return data, nil }