func (h DockerHandler) HandleHTTP(w http.ResponseWriter, r *http.Request) { conn, err := net.Dial("unix", h.dockerEndpoint) if err != nil { helpers.ErrorLog("DOCKER", fmt.Sprintf("Following error occured when trying to connect Docker endpoint %s : %s", h.dockerEndpoint, err.Error())) return } // Creating a socket client clientConn := httputil.NewClientConn(conn, nil) // Forwarding request to client resp, err := clientConn.Do(r) if err != nil { helpers.ErrorLog("DOCKER", fmt.Sprintf("Following error occured when trying to make request to Docker endpoint %s : %s", h.dockerEndpoint, err.Error())) return } for kA, vA := range resp.Header { for _, vB := range vA { w.Header().Add(kA, vB) } } io.Copy(w, resp.Body) resp.Body.Close() clientConn.Close() conn.Close() }
func (h DockerHandler) HandleWebSocket(w http.ResponseWriter, r *http.Request) { dockerConn, err := h.GetWebSocket(w, r) if err != nil { helpers.ErrorLog("DOCKER", fmt.Sprintf("Following error occured when trying to access Docker websocket %s : %s", r.URL, err.Error())) return } else { websocket.Handler(func(ws *websocket.Conn) { var wg sync.WaitGroup wg.Add(2) // From docker to client go func() { io.Copy(ws, dockerConn) dockerConn.Close() ws.Close() wg.Done() }() // From client to docker go func() { io.Copy(dockerConn, ws) dockerConn.Close() ws.Close() wg.Done() }() wg.Wait() }).ServeHTTP(w, r) } }
func (h KitematicHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { url := fmt.Sprintf("%s", r.URL) res, err := http.Get("http://kitematic.com" + url) if err != nil { helpers.ErrorLog("KITEMATIC", fmt.Sprintf("Following error occured when trying to access %s : %s", url, err.Error())) return } io.Copy(w, res.Body) }
func (h DockerHandler) GetWebSocket(w http.ResponseWriter, r *http.Request) (*websocket.Conn, error) { client, err := net.Dial("unix", h.dockerEndpoint) if err != nil { helpers.ErrorLog("DOCKER", fmt.Sprintf("Following error occured when trying to make request to Docker endpoint %s : %s", h.dockerEndpoint, err.Error())) return nil, err } config := &websocket.Config{} config.Version = websocket.ProtocolVersionHybi13 config.Location = r.URL config.Origin = r.URL config.Header = r.Header return websocket.NewClient(config, client) }
func main() { args := os.Args if len(args) > 1 { switch args[1] { case "encrypt": if len(args) < 3 { helpers.ErrorLog("ENCRYPT", "You need to provide at least 2 arguments") } else { password := []byte(args[2]) hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost) if err != nil { helpers.ErrorLog("ENCRYPT", fmt.Sprintf("Following error occured when trying to encrypt password : %s", err.Error())) } fmt.Println(string(hashedPassword)) } default: break } return } var config configuration.Configuration config.Load() config.Log() handler := server.CreateHandlers(config) if config.UseSSL { // Encapsulated in subroutine to listen both HTTP and HTTPs go func() { log.Println(http.ListenAndServeTLS(config.ListenHTTPS, config.SSLCertPath, config.SSLKeyPath, handler)) }() } log.Println(http.ListenAndServe(config.ListenHTTP, handler)) }