forked from skbkontur/openvpn-auth-radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
75 lines (60 loc) · 2.59 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
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Configuration;
using FP.Radius;
namespace auth
{
class Program
{
static int Main(string[] args)
{
var username = Environment.GetEnvironmentVariable("username");
var password = Environment.GetEnvironmentVariable("password");
if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
{
Console.WriteLine("environment variables username or password undefined");
return 1;
}
if(Config.Settings == null)
{
Console.WriteLine("Config is empty/unreadable");
return 1;
}
if (Config.Settings.Servers == null || Config.Settings.Servers.Count == 0)
{
Console.WriteLine("No servers found in config");
return 1;
}
var res = Parallel.ForEach(Config.Settings.Servers.Cast<ServerElement>(), (server, state) =>
{
// Console.WriteLine("server.name = {0}, sharedsecret={1}, retries={2}, wait={3}, authport={4}", server.Name, server.sharedsecret, server.retries, server.wait, server.authport);
var rc = new RadiusClient(server.Name, server.sharedsecret, server.wait * 1000, server.authport);
var authPacket = rc.Authenticate(username, password);
if (Config.Settings.NAS_IDENTIFIER != null)
authPacket.SetAttribute(new RadiusAttribute(RadiusAttributeType.NAS_IDENTIFIER, Encoding.ASCII.GetBytes(Config.Settings.NAS_IDENTIFIER)));
authPacket.SetAttribute(new RadiusAttribute(RadiusAttributeType.NAS_PORT_TYPE, BitConverter.GetBytes((int)NasPortType.ASYNC)));
for (int i = 0; i < server.retries; i++)
try
{
var receivedPacket = rc.SendAndReceivePacket(authPacket).Result;
if (receivedPacket != null && receivedPacket.PacketType == RadiusCode.ACCESS_ACCEPT)
state.Stop();
}
catch { }
});
if (res.IsCompleted)
{
Console.WriteLine("Auth failed");
return 1;
}
else
{
Console.WriteLine("Auth Ok");
return 0;
}
}
}
}