Hi,

Since golang is so awesome in it's network abilities, I wrote a simple 
inetd super server, but my network connections come encrypted with SSL, 
instead of plain-text.  This program runs great as it is, my client 
communicates and send and receives data from the server's exec-ed program, 
but the problem is that the server exec-ed program does not sense a close 
on stdin when the client closes the network connection.  In the log, I will 
see the 'Start received', but I will never see the 'Command terminated', 
and the processes are still running.

Thank you for any help or suggestions,

Kevin Johnson


Here is the code:

package main


import (

    "crypto/rand"

    "crypto/tls"

    "crypto/x509"

    "io"

    "io/ioutil"

    "log"

    "net"

    "os"

    "os/exec"

)


func main() {


    if len(os.Args) > 1 {

        dogen()

        return

    }


    log.SetFlags(log.Lshortfile)


    ca_b, _ := ioutil.ReadFile("/etc/sinetd/ca.pem")

    ca, _ := x509.ParseCertificate(ca_b)

    priv_b, _ := ioutil.ReadFile("/etc/sinetd/ca.key")

    priv, _ := x509.ParsePKCS1PrivateKey(priv_b)


    pool := x509.NewCertPool()

    pool.AddCert(ca)


    cert := tls.Certificate{

        Certificate: [][]byte{ca_b},

        PrivateKey:  priv,

    }


    config := tls.Config{

        ClientAuth:   tls.NoClientCert,

        Certificates: []tls.Certificate{cert},

        ClientCAs:    pool,

    }


    config.Rand = rand.Reader


    listener, err := tls.Listen("tcp", ":823", &config)

    if err != nil {

        log.Fatal(err)

    }


    defer listener.Close()


    for {

        conn, err := listener.Accept()

        if err != nil {

            log.Println(err)

            continue

        }

        go handleConnection(conn)

    }

}


func handleConnection(conn net.Conn) {

    defer conn.Close()


    // read to make sure they are who we want them to be

    buf := make([]byte, 512)

    n, err := conn.Read(buf)

    if err != nil {

        log.Print(err)

    }


    if n > 0 {

        cmd := string(buf[:n-1])

        if cmd == "START" {

            log.Println("Start received")

            cmd := exec.Command("/usr/local/sbin/MyService")


            cmd.Stdin = conn

            cmd.Stdout = conn


            if err := cmd.Run(); err != nil {

                log.Print(err)

            }


            log.Println("Command terminated")


        } else {

            log.Println("Unknown command:", cmd)

        }

    }

}


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to