Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Srelay - the SOCKS proxy and Relay (with Randomized Balancing)
==============================================================
phoeagon

Added *randomized* downstream balancing: when multiple rules apply, choose any
in a random way.
Added *prioritized random* downstreaming: when multiple downstreams available,
maintain a list of priorities of each downstream. Penalize a downstream if
a connection to it fails. Choose a random downstream in prioritized way. (Useful
for load-balancing multiple SOCKS5 proxy server).

Eg:

srelay -i 0.0.0.0:9999 \ # Listen on 9999 port
-P -R \ # Randomized, prioritized
-c config.conf \ # Choose a config file
-f # Stay forground.

The original repo was imported from
[sourceforge](http://socks-relay.sourceforge.net/).

What is it?
============

* Srelay is a socks 4/5 protocol proxy server.
* Supports socks connect/bind request in the protocol v4, v4a, and v5.
* Supports socks server chaining with both v4 and v5 servers.
* Supports Username/Password authentication in v5 (not recommended).
* Testing on FreeBSD 8.1R, Solaris 8, 10, Linux-i386, MacOS 10.5.
* Supports IPv6 as well as IPv4.
* Srelay is Free.

Documents
=========

Config Sample: [basic config](http://socks-relay.sourceforge.net/samples.html)

References
==========
(These document links do not assure the compliancy of this software. Yeah, indeed.)

* [SOCKS Protocol Version 4](http://socks-relay.sourceforge.net/socks4.protocol.txt)
* [SOCKS Protocol Version 4A socks 4a](http://socks-relay.sourceforge.net/socks4a.protocol.txt)
* SOCKS Protocol Version 5 [RFC 1928](http://www.ietf.org/rfc/rfc1928.txt)
* Username/Password Authentication for SOCKS V5 [RFC 1929](http://www.ietf.org/rfc/rfc1929.txt)
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -4297,7 +4297,7 @@ cat >>conftest.$ac_ext <<_ACEOF
void
init_routine()
{
return(0);
return;
}
main()
{
Expand Down
2 changes: 1 addition & 1 deletion configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ if test "$thread" != "no"; then
void
init_routine()
{
return(0);
return;
}
main()
{
Expand Down
12 changes: 6 additions & 6 deletions get-bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ int get_bind_addr(bin_addr *dest, struct addrinfo *ba)

close(s);
/*
msg_out(norm,"nlmsg_pid: %d, nlmsg_seq: %d\n",
msg_out(norm,"nlmsg_pid: %d, nlmsg_seq: %d",
h->nlmsg_pid, h->nlmsg_seq);
*/
len = h->nlmsg_len;
Expand All @@ -437,11 +437,11 @@ int get_bind_addr(bin_addr *dest, struct addrinfo *ba)
/*
if (tb[RTA_DST]) {
inet_ntop(AF_INET, RTA_DATA(tb[RTA_DST]), str, sizeof(str));
msg_out(norm, "DST %s\n", str);
msg_out(norm, "DST %s", str);
}
if (tb[RTA_GATEWAY]) {
inet_ntop(AF_INET, RTA_DATA(tb[RTA_GATEWAY]), str, sizeof(str));
msg_out(norm, "GW %s\n", str);
msg_out(norm, "GW %s", str);
}
*/
if (tb[RTA_OIF]) {
Expand Down Expand Up @@ -501,7 +501,7 @@ int get_ifconf(int index, struct addrinfo *ba)

close(s);
/*
msg_out(norm,"nlmsg_pid: %d, nlmsg_seq: %d\n",
msg_out(norm,"nlmsg_pid: %d, nlmsg_seq: %d",
h->nlmsg_pid, h->nlmsg_seq);
*/
while (NLMSG_OK(h, status)) {
Expand All @@ -519,7 +519,7 @@ int get_ifconf(int index, struct addrinfo *ba)
/*
char str[128];
inet_ntop(AF_INET, RTA_DATA(tb[IFA_ADDRESS]), str, sizeof(str));
msg_out(norm, "ADDRESS %s\n", str);
msg_out(norm, "ADDRESS %s", str);
*/
ba->ai_family = AF_INET; /* IPv4 */
ba->ai_socktype = SOCK_STREAM;
Expand All @@ -533,7 +533,7 @@ int get_ifconf(int index, struct addrinfo *ba)
/*
if (tb[IFA_LOCAL]) {
unsigned *d = RTA_DATA(tb[IFA_LOCAL]);
msg_out(norm, "LOCAL %08x\n", *d);
msg_out(norm, "LOCAL %08x", *d);
}
*/
}
Expand Down
22 changes: 15 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
main.c:
$Id$
$Id: main.c,v 1.22 2010/12/20 14:12:00 bulkstream Exp $

Copyright (C) 2001-2010 Tomo.M (author).
All rights reserved.
Expand Down Expand Up @@ -66,6 +66,8 @@ int deny_severity = LOG_AUTH|LOG_NOTICE;
extern int hosts_ctl __P((char *, char *, char *, char *));
#endif /* HAVE_LIBWRAP */

int prioritize_downstreams = 0; /* If multiple apply, pick by priorities.*/
int random_downstream = 0; /* If multiple rules apply, pick a random one. */
int max_child;
int cur_child;

Expand Down Expand Up @@ -99,6 +101,8 @@ void usage()
"\t-a np\tauth methods n: no, p:pass\n"
"\t-u file\tsrelay password file\n"
"\t-f\trun into foreground\n"
"\t-P\tmaintain priority list of downstreams when multiple apply, implies -R\n"
"\t-R\tpick a random downstream if multiply fits\n"
"\t-r\tresolve client name in log\n"
"\t-s\tforce logging to syslog\n"
"\t-t\tdisable threading\n"
Expand All @@ -108,7 +112,6 @@ void usage()
"\t-w\tuse tcp_wrapper access control\n"
#endif /* HAVE_LIBWRAP */
"\t-I\tinetd mode\n"
"\t-q\twill be quiet\n"
"\t-v\tshow version and exit\n"
"\t-h\tshow this help and exit\n");
exit(1);
Expand Down Expand Up @@ -431,7 +434,7 @@ int main(int ac, char **av)

openlog(ident, LOG_PID | LOG_NDELAY, SYSLOGFAC);

while((ch = getopt(ac, av, "a:c:i:J:m:o:p:u:frstbwgIqvh?")) != -1)
while((ch = getopt(ac, av, "a:c:i:J:m:o:p:u:fRPrstbwgIvh?")) != -1)
switch (ch) {
case 'a':
if (optarg != NULL) {
Expand Down Expand Up @@ -465,6 +468,15 @@ int main(int ac, char **av)
bind_restrict = 0;
break;

case 'P':
prioritize_downstreams = 1;
random_downstream = 1;
break;

case 'R':
random_downstream = 1;
break;

case 'c':
if (optarg != NULL) {
config = strdup(optarg);
Expand Down Expand Up @@ -547,10 +559,6 @@ int main(int ac, char **av)
inetd_mode = 1;
break;

case 'q':
be_quiet = 1;
break;

case 'v':
show_version();
exit(1);
Expand Down
2 changes: 1 addition & 1 deletion readconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ void add_entry(ROUTE_INFO *r, ROUTE_INFO *t, int ind)

void parse_err(int sev, int line, char *msg)
{
msg_out(sev, "%s: line %d: %s\n", CONFIG, line, msg);
msg_out(sev, "%s: line %d: %s", CONFIG, line, msg);
}

int str_to_addr(char *addr, bin_addr *dest)
Expand Down
32 changes: 26 additions & 6 deletions relay.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
relay.c:
$Id$
$Id: relay.c,v 1.20 2010/11/05 02:13:12 bulkstream Exp $

Copyright (C) 2001-2010 Tomo.M (author).
All rights reserved.
Expand Down Expand Up @@ -61,6 +61,14 @@ int decode_socks_udp __P((UDP_ATTR *, u_char *));
void relay_tcp __P((SOCKS_STATE *));
void relay_udp __P((SOCKS_STATE *));
int log_transfer __P((SOCK_INFO *, LOGINFO *));
void soft_penal(int index);
void encourage(int index, int delta);

int mathlog( int x ){
int r = 0;
while (x >0) { r++; x/=2; }
return r;
}

void readn(rlyinfo *ri)
{
Expand Down Expand Up @@ -314,33 +322,38 @@ void relay_tcp(SOCKS_STATE *state)
ri.from = state->r; ri.to = state->s; ri.flags = 0;
if ((wc = forward(&ri)) <= 0)
done++;
else
else {
li.bc += wc; li.dnl += wc;

}
FD_CLR(state->r, &rfds);
}
if (FD_ISSET(state->r, &xfds)) {
ri.from = state->r; ri.to = state->s; ri.flags = MSG_OOB;
if ((wc = forward(&ri)) <= 0)
done++;
else
else {
li.bc += wc; li.dnl += wc;
}
if ( wc < 0 )
soft_penal(state->tbl_ind);
FD_CLR(state->r, &xfds);
}
if (FD_ISSET(state->s, &rfds)) {
ri.from = state->s; ri.to = state->r; ri.flags = 0;
if ((wc = forward(&ri)) <= 0)
done++;
else
else {
li.bc += wc; li.upl += wc;
}
FD_CLR(state->s, &rfds);
}
if (FD_ISSET(state->s, &xfds)) {
ri.from = state->s; ri.to = state->r; ri.flags = MSG_OOB;
if ((wc = forward(&ri)) <= 0)
done++;
else
else {
li.bc += wc; li.upl += wc;
}
FD_CLR(state->s, &xfds);
}
if (done > 0)
Expand All @@ -356,6 +369,13 @@ void relay_tcp(SOCKS_STATE *state)
}
}
}
fprintf(stderr, " proxy %d: up=%d, down=%d\n", state->tbl_ind, li.upl, li.dnl);
if (li.dnl < 10 && li.upl > 100) {
// Error: Timeout or something
soft_penal(state->tbl_ind);
} else if ( li.dnl > li.upl ) {
encourage(state->tbl_ind, (li.dnl/(li.upl+1)) * 64);
}
gettimeofday(&li.end, &tz);
log_transfer(state->si, &li);

Expand Down
2 changes: 1 addition & 1 deletion scripts/rc.srelay
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
# startup script for srelay
# Tomo.M
# $Id$
# $Id: rc.srelay,v 1.1 2009/09/02 13:41:40 bulkstream Exp $
# chkconfig: 345 99 0
# description: Srelay is a SOCKS proxy.

Expand Down
2 changes: 1 addition & 1 deletion scripts/srelay.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
# startup script for srelay
# srelay_enable="Yes" in /etc/rc.conf needed for enable.
# $Id$
# $Id: srelay.sh,v 1.1 2009/09/02 13:41:40 bulkstream Exp $
# Tomo.M

# PROVIDE: srelay
Expand Down
2 changes: 1 addition & 1 deletion scripts/svc-srelay
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
# startup script for srelay
# $Id$
# $Id: svc-srelay,v 1.1 2009/09/02 13:41:40 bulkstream Exp $
# Tomo.M

name="srelay"
Expand Down
Loading