summaryrefslogtreecommitdiffstats
path: root/ship/lib/network
blob: e1a9a31e3d05955f47605208ec9a166e3129acdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#@include core

# TODO refactor this
which_get_loader(){
  if ! exists curl ; then
    if ! exists wget ; then
      warn "Please install curl or wget"
      return 1
    else
      echo "wget -O-"
    fi
  else
    echo "curl"
  fi
  return 0
}

which_head_loader(){
  if ! exists curl ; then
    if ! exists wget ; then
      warn "Please install curl or wget"
      return 1
    else
      echo "wget -O- --spider"
    fi
  else
    echo "curl -I"
  fi
  return 0
}

http_get(){
    "$(which_get_loader)" "${1?please provide url}"
}
http_head(){
    "$(which_head_loader)" "${1?please provide url}"
}

which_telnet(){
  # find Telnet or similar and executes it at the end
  # requires exist
  # if env TELNET is set, will be trying to run this 
  # Tries the following things:
  #     telnet
  #     nc
  #     netcat
  #     busybox telnet
  if [ -e "${TELNET:-does_not_exist}" ]; then
    info "Will be using $TELNET as Telnet Client"
    echo $TELNET
  elif exists telnet ;then
    command -v telnet
  elif exists nc ;then
    command -v nc
  elif exists netcat;then
    command -v netcat
  elif exists busybox;then
    echo  `command -v busybox` telnet
  else
    die 'Cannot find telnet binary, please install either telnet-client or busybox or netcat or provided TELNET environment.'
  fi
}

run_telnet(){
  host="$1"
  port="$2"
  $(which_telnet) "$host" "$port"
}

send_irc(){
  ## reads from stdin, writes to IRC
  ##
  ## requires func: exists() anytelnet()
  if [ -z "${HOSTN:-}" ]; then
    HOSTN="$(get_hostname)"
    info "no HOSTN given, using $HOSTN instead"
  fi
  IRCCHANNEL=${IRCCHANNEL:-"#krebs_incoming"}
  IRCSERVER=${IRCSERVER:-"irc.freenode.net"}
  IRCPORT=${IRCPORT:-6667}
  NICK="${HOSTN}_$(head /dev/urandom | tr -dc "0123456789" | head -c3)"
  info "starting irc connect as $NICK"
  (   echo "NICK $NICK";
      echo "USER $NICK $IRCSERVER bla : $NICK";
      echo "JOIN $IRCCHANNEL";
      sleep 23;
      while read line; do echo "PRIVMSG $IRCCHANNEL :$line";sleep 1;done
      sleep 5; ) | run_telnet $IRCSERVER $IRCPORT 2>/dev/null | line_to_dot
}