| 56 | } |
| 57 | |
| 58 | func (self *RpcEngine) Start() { |
| 59 | log.WithFields(log.Fields{ |
| 60 | "port": self.port, |
| 61 | }).Info("Starting RPC server") |
| 62 | |
| 63 | l, e := net.Listen("tcp", ":"+self.port) |
| 64 | if e != nil { |
| 65 | log.Fatal("Could not bind on port:", e) |
| 66 | } |
| 67 | |
| 68 | http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 69 | username, password, _ := r.BasicAuth() |
| 70 | |
| 71 | log.Debugf("received username %s and password %s", username, password) |
| 72 | if username != self.config.ApiKey || password != self.config.ApiSecret { |
| 73 | log.Debug("Wrong authentication, not processing") |
| 74 | log.Debugf("Expected %s and %s", self.config.ApiKey, self.config.ApiSecret) |
| 75 | w.WriteHeader(401) |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | if r.URL.Path == "/rpc" { |
| 80 | log.Debug("Received call to RPC interface") |
| 81 | body, err := ioutil.ReadAll(r.Body) |
| 82 | if err == nil { |
| 83 | log.Debugf("HTTP Body: %s", body) |
| 84 | } |
| 85 | // Place the data back in the buffer so we can process it normally |
| 86 | r.Body = ioutil.NopCloser(bytes.NewBuffer(body)) |
| 87 | |
| 88 | serverCodec := jsonrpc.NewServerCodec(&HttpConn{in: r.Body, out: w}) |
| 89 | w.Header().Set("Content-type", "application/json") |
| 90 | w.Header().Set("Access-Control-Allow-Origin", "*") |
| 91 | w.WriteHeader(200) |
| 92 | err = self.server.ServeRequest(serverCodec) |
| 93 | if err != nil { |
| 94 | log.Printf("Error while serving JSON request: %v", err) |
| 95 | return |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | })) |
| 100 | } |
| 101 | |
| 102 | func (self *RpcEngine) Activate(p plugins.Plugin) { |
| 103 | log.WithFields(log.Fields{ |