Last active
December 20, 2015 09:29
-
-
Save madmo/6107721 to your computer and use it in GitHub Desktop.
Simple tcp forwarding daemon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2013, Moritz Bitsch <moritzbitsch@googlemail.com> | |
// | |
// Permission to use, copy, modify, and/or distribute this software for any | |
// purpose with or without fee is hereby granted, provided that the above | |
// copyright notice and this permission notice appear in all copies. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
package main | |
import ( | |
"bitbucket.org/madmo/daemonize" | |
"flag" | |
"fmt" | |
"io" | |
"net" | |
"os" | |
"os/signal" | |
"syscall" | |
) | |
var ( | |
optListenAddress = flag.String("l", ":443", "Set the listen address") | |
optConnectAddress = flag.String("c", "127.0.0.1:22", "Connect address") | |
optNoDaemon = flag.Bool("D", false, "Do not fork into background") | |
) | |
func dropRoot() { | |
if syscall.Getuid() == 0 { | |
syscall.Setgid(65534) | |
syscall.Setuid(65534) | |
} | |
if syscall.Getuid() == 0 { | |
panic("still root") | |
} | |
} | |
func HandleConnection(c net.Conn) { | |
defer c.Close() | |
cc, err := net.Dial("tcp", *optConnectAddress) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Can't connect: %v\n", cc) | |
return | |
} | |
defer cc.Close() | |
go io.Copy(c, cc) | |
io.Copy(cc, c) | |
} | |
func main() { | |
flag.Parse() | |
if !*optNoDaemon { | |
p, err := daemonize.Daemonize(false, false) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Can't daemonize: %v\n") | |
return | |
} | |
if p != nil { | |
return | |
} | |
} | |
sig := make(chan os.Signal) | |
signal.Notify(sig, os.Interrupt) | |
l, err := net.Listen("tcp", *optListenAddress) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Can't listen: %v\n", err) | |
return | |
} | |
defer l.Close() | |
dropRoot() | |
go func() { | |
for { | |
c, err := l.Accept() | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Can't accept: %v\n", err) | |
return | |
} | |
HandleConnection(c) | |
} | |
}() | |
<-sig | |
fmt.Println("Got Signal, exiting") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment