-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
192 lines (171 loc) · 6.18 KB
/
Program.cs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using NAudio.Wave;
using NDesk.Options;
using System.Speech.Synthesis;
namespace AudioGhost
{
class Program
{
static int verbosity;
static void Debug(string format, params object[] args)
{
if (verbosity > 0)
{
Console.Write("# ");
Console.WriteLine(format, args);
}
}
static void ParseCommandLine(string[] args)
{
bool showHelp = false;
string maskPrefix = null;
var p = new OptionSet() {
{ "h|?|help", "show this message and exit", v => showHelp = v != null },
{ "m|mask=", "the first two octets of the subnet mask", v => maskPrefix = v },
};
List<string> extra = p.Parse(args);
if (showHelp)
{
ShowHelp(p);
return;
}
string message = null;
if (extra.Count > 0)
{
message = string.Join(" ", extra.ToArray());
Debug("Using new message: {0}", message);
}
else
{
message = "You will never guess from where this is coming.";
Debug("Using default message: {0}", message);
}
}
static void PlayUsingNAudio(String soundFile)
{
// The hard way.
// http://naudio.codeplex.com/documentation
// http://naudio.codeplex.com/wikipage?title=WAV
using (var wfr = new WaveFileReader(soundFile))
using (WaveChannel32 wc = new WaveChannel32(wfr) { PadWithZeroes = false })
using (var audioOutput = new DirectSoundOut())
{
audioOutput.Init(wc);
audioOutput.Play();
while (audioOutput.PlaybackState != PlaybackState.Stopped)
{
Thread.Sleep(20);
}
audioOutput.Stop();
}
}
static void PlayUsingSoundPlayer(String soundFile)
{
// The easy way.
// http://stackoverflow.com/questions/3502311/how-to-play-a-sound-in-c-net
// http://stackoverflow.com/questions/5756855/c-sharp-play-sound-with-one-line-of-c-sharp-code
System.Media.SoundPlayer player = new System.Media.SoundPlayer(soundFile);
player.Play();
}
static void ShowHelp(OptionSet p)
{
Console.WriteLine("Usage: ag [OPTIONS]+ message");
Console.WriteLine("Options:");
p.WriteOptionDescriptions(Console.Out);
}
static void Send(string hostName, int port, string message)
{
// http://www.csharp-examples.net/socket-send-receive/
//TcpClient tcpClient = new TcpClient();
//Socket socket = tcpClient.Client;
//string hello = "Hello world!";
//try
//{
// GhostClient.Send(socket, Encoding.UTF8.GetBytes(hello), 0, hello.Length, 10000);
//}
//catch (Exception)
//{
// // Do nothing.
//}
TcpClient tcpClient = new TcpClient(hostName, port);
Byte[] data = Encoding.UTF8.GetBytes(message);
NetworkStream stream = tcpClient.GetStream();
stream.Write(data, 0, data.Length);
Debug("Sent: {0}", message);
stream.Close();
tcpClient.Close();
}
static void Main(string[] args)
{
int port = 8085;
bool showHelp = false;
bool useLocal = false;
string server = "localhost";
string maskPrefix = "10.21";
var p = new OptionSet() {
{ "h|?|help", "show this message and exit", v => showHelp = v != null },
{ "l|local", "send to localhost", v => useLocal = v != null },
{ "m|mask=", "the first two octets of the subnet mask", v => maskPrefix = v },
{ "p|port=", "the TCP port to which to send", (int v) => port = v },
{ "s|server=", "the host to which to send", v => server = v },
{ "v|verbose", "", v => { if (v != null) ++verbosity; } },
};
List<string> extra = p.Parse(args);
if (showHelp)
{
ShowHelp(p);
return;
}
string message = null;
if (extra.Count > 0)
{
message = string.Join(" ", extra.ToArray());
Debug("Using new message: {0}", message);
}
else
{
message = "You will never guess from where this is coming.";
Debug("Using default message: {0}", message);
}
if (verbosity > 0)
{
int index = 0;
string hostName = Dns.GetHostName();
IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
IPAddress[] addys = hostEntry.AddressList;
IPAddress bindTo = null;
Console.WriteLine("\n NICS");
foreach (IPAddress a in addys)
{
if (a.ToString().StartsWith(maskPrefix))
{
bindTo = a;
}
Console.WriteLine(" [{0}] {1}", index, a);
index++;
}
Console.WriteLine("\nWe should bind to {0}", bindTo);
}
if (useLocal)
{
// http://code.msdn.microsoft.com/windowsdesktop/Text-to-Speech-Converter-0ed77dd5
SpeechSynthesizer reader = new SpeechSynthesizer();
reader.Speak(message);
}
else
{
Send(server, port, message);
}
var soundFile = "tweet.wav";
PlayUsingNAudio(soundFile);
// Pause...
Console.ReadLine();
}
}
}