| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- /*
- * Copyright (c) 2016, Psiphon Inc.
- * All rights reserved.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- package server
- import (
- "crypto/tls"
- "encoding/json"
- "fmt"
- "io/ioutil"
- golanglog "log"
- "net"
- "net/http"
- "sync"
- "time"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
- )
- const WEB_SERVER_IO_TIMEOUT = 10 * time.Second
- type webServer struct {
- support *SupportServices
- serveMux *http.ServeMux
- }
- // RunWebServer runs a web server which supports tunneled and untunneled
- // Psiphon API requests.
- //
- // The HTTP request handlers are light wrappers around the base Psiphon
- // API request handlers from the SSH API transport. The SSH API transport
- // is preferred by new clients; however the web API transport is still
- // required for untunneled final status requests. The web API transport
- // may be retired once untunneled final status requests are made obsolete
- // (e.g., by server-side bytes transferred stats, by client-side local
- // storage of stats for retry, or some other future development).
- //
- // The API is compatible with all tunnel-core clients but not backwards
- // compatible with older clients.
- //
- func RunWebServer(
- support *SupportServices,
- shutdownBroadcast <-chan struct{}) error {
- webServer := &webServer{
- support: support,
- }
- serveMux := http.NewServeMux()
- serveMux.HandleFunc("/handshake", webServer.handshakeHandler)
- serveMux.HandleFunc("/connected", webServer.connectedHandler)
- serveMux.HandleFunc("/status", webServer.statusHandler)
- serveMux.HandleFunc("/client_verification", webServer.clientVerificationHandler)
- certificate, err := tls.X509KeyPair(
- []byte(support.Config.WebServerCertificate),
- []byte(support.Config.WebServerPrivateKey))
- if err != nil {
- return common.ContextError(err)
- }
- tlsConfig := &tls.Config{
- Certificates: []tls.Certificate{certificate},
- }
- // TODO: inherits global log config?
- logWriter := NewLogWriter()
- defer logWriter.Close()
- // Note: WriteTimeout includes time awaiting request, as per:
- // https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts
- server := &HTTPSServer{
- http.Server{
- MaxHeaderBytes: MAX_API_PARAMS_SIZE,
- Handler: serveMux,
- TLSConfig: tlsConfig,
- ReadTimeout: WEB_SERVER_IO_TIMEOUT,
- WriteTimeout: WEB_SERVER_IO_TIMEOUT,
- ErrorLog: golanglog.New(logWriter, "", 0),
- },
- }
- listener, err := net.Listen(
- "tcp", fmt.Sprintf("%s:%d",
- support.Config.ServerIPAddress,
- support.Config.WebServerPort))
- if err != nil {
- return common.ContextError(err)
- }
- log.WithContext().Info("starting")
- err = nil
- errors := make(chan error)
- waitGroup := new(sync.WaitGroup)
- waitGroup.Add(1)
- go func() {
- defer waitGroup.Done()
- // Note: will be interrupted by listener.Close()
- err := server.ServeTLS(listener)
- // Can't check for the exact error that Close() will cause in Accept(),
- // (see: https://code.google.com/p/go/issues/detail?id=4373). So using an
- // explicit stop signal to stop gracefully.
- select {
- case <-shutdownBroadcast:
- default:
- if err != nil {
- select {
- case errors <- common.ContextError(err):
- default:
- }
- }
- }
- log.WithContext().Info("stopped")
- }()
- select {
- case <-shutdownBroadcast:
- case err = <-errors:
- }
- listener.Close()
- waitGroup.Wait()
- log.WithContext().Info("exiting")
- return err
- }
- // convertHTTPRequestToAPIRequest converts the HTTP request query
- // parameters and request body to the JSON object import format
- // expected by the API request handlers.
- func convertHTTPRequestToAPIRequest(
- w http.ResponseWriter,
- r *http.Request,
- requestBodyName string) (requestJSONObject, error) {
- params := make(requestJSONObject)
- for name, values := range r.URL.Query() {
- for _, value := range values {
- // Note: multiple values per name are ignored
- // TODO: faster lookup?
- isArray := false
- for _, paramSpec := range baseRequestParams {
- if paramSpec.name == name {
- isArray = (paramSpec.flags&requestParamArray != 0)
- break
- }
- }
- if isArray {
- // Special case: a JSON encoded array
- var arrayValue []interface{}
- err := json.Unmarshal([]byte(value), &arrayValue)
- if err != nil {
- return nil, common.ContextError(err)
- }
- params[name] = arrayValue
- } else {
- // All other query parameters are simple strings
- params[name] = value
- }
- break
- }
- }
- if requestBodyName != "" {
- r.Body = http.MaxBytesReader(w, r.Body, MAX_API_PARAMS_SIZE)
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return nil, common.ContextError(err)
- }
- // TODO: can't use requestJSONObject type?
- var bodyParams map[string]interface{}
- err = json.Unmarshal(body, &bodyParams)
- if err != nil {
- return nil, common.ContextError(err)
- }
- params[requestBodyName] = bodyParams
- }
- return params, nil
- }
- func (webServer *webServer) lookupGeoIPData(params requestJSONObject) GeoIPData {
- clientSessionID, err := getStringRequestParam(params, "client_session_id")
- if err != nil {
- // Not all clients send this parameter
- return NewGeoIPData()
- }
- return webServer.support.GeoIPService.GetSessionCache(clientSessionID)
- }
- func (webServer *webServer) handshakeHandler(w http.ResponseWriter, r *http.Request) {
- params, err := convertHTTPRequestToAPIRequest(w, r, "")
- var responsePayload []byte
- if err == nil {
- responsePayload, err = handshakeAPIRequestHandler(
- webServer.support, webServer.lookupGeoIPData(params), params)
- }
- if err != nil {
- log.WithContextFields(LogFields{"error": err}).Warning("failed")
- w.WriteHeader(http.StatusNotFound)
- return
- }
- // The legacy response format is newline seperated, name prefixed values.
- // Within that legacy format, the modern JSON response (containing all the
- // legacy response values and more) is single value with a "Config:" prefix.
- // This response uses the legacy format but omits all but the JSON value.
- responseBody := append([]byte("Config: "), responsePayload...)
- w.WriteHeader(http.StatusOK)
- w.Write(responseBody)
- }
- func (webServer *webServer) connectedHandler(w http.ResponseWriter, r *http.Request) {
- params, err := convertHTTPRequestToAPIRequest(w, r, "")
- var responsePayload []byte
- if err == nil {
- responsePayload, err = connectedAPIRequestHandler(
- webServer.support, webServer.lookupGeoIPData(params), params)
- }
- if err != nil {
- log.WithContextFields(LogFields{"error": err}).Warning("failed")
- w.WriteHeader(http.StatusNotFound)
- return
- }
- w.WriteHeader(http.StatusOK)
- w.Write(responsePayload)
- }
- func (webServer *webServer) statusHandler(w http.ResponseWriter, r *http.Request) {
- params, err := convertHTTPRequestToAPIRequest(w, r, "statusData")
- if err == nil {
- _, err = statusAPIRequestHandler(
- webServer.support, webServer.lookupGeoIPData(params), params)
- }
- if err != nil {
- log.WithContextFields(LogFields{"error": err}).Warning("failed")
- w.WriteHeader(http.StatusNotFound)
- return
- }
- w.WriteHeader(http.StatusOK)
- }
- func (webServer *webServer) clientVerificationHandler(w http.ResponseWriter, r *http.Request) {
- params, err := convertHTTPRequestToAPIRequest(w, r, "verificationData")
- if err == nil {
- _, err = clientVerificationAPIRequestHandler(
- webServer.support, webServer.lookupGeoIPData(params), params)
- }
- if err != nil {
- log.WithContextFields(LogFields{"error": err}).Warning("failed")
- w.WriteHeader(http.StatusNotFound)
- return
- }
- w.WriteHeader(http.StatusOK)
- }
|