File: auth_async.py

package info (click to toggle)
pyrad 2.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 932 kB
  • sloc: python: 4,021; makefile: 15
file content (164 lines) | stat: -rw-r--r-- 4,683 bytes parent folder | download
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
#!/usr/bin/python

import asyncio

import logging
import traceback
from pyrad.dictionary import Dictionary
from pyrad.client_async import ClientAsync
from pyrad.packet import AccessAccept

logging.basicConfig(level="DEBUG",
                    format="%(asctime)s [%(levelname)-8s] %(message)s")
client = ClientAsync(server="localhost",
                     secret=b"Kah3choteereethiejeimaeziecumi",
                     timeout=4,
                     dict=Dictionary("dictionary"))

loop = asyncio.get_event_loop()


def create_request(client, user):
    req = client.CreateAuthPacket(User_Name=user)

    req["NAS-IP-Address"] = "192.168.1.10"
    req["NAS-Port"] = 0
    req["Service-Type"] = "Login-User"
    req["NAS-Identifier"] = "trillian"
    req["Called-Station-Id"] = "00-04-5F-00-0F-D1"
    req["Calling-Station-Id"] = "00-01-24-80-B3-9C"
    req["Framed-IP-Address"] = "10.0.0.100"

    return req

def print_reply(reply):
    if reply.code == AccessAccept:
        print("Access accepted")
    else:
        print("Access denied")

    print("Attributes returned by server:")
    for i in reply.keys():
        print("%s: %s" % (i, reply[i]))

def test_auth1():

    global client

    try:
        # Initialize transports
        loop.run_until_complete(
            asyncio.ensure_future(
                client.initialize_transports(enable_auth=True,
                                             local_addr='127.0.0.1',
                                             local_auth_port=8000,
                                             enable_acct=True,
                                             enable_coa=True)))



        req = client.CreateAuthPacket(User_Name="wichert")

        req["NAS-IP-Address"] = "192.168.1.10"
        req["NAS-Port"] = 0
        req["Service-Type"] = "Login-User"
        req["NAS-Identifier"] = "trillian"
        req["Called-Station-Id"] = "00-04-5F-00-0F-D1"
        req["Calling-Station-Id"] = "00-01-24-80-B3-9C"
        req["Framed-IP-Address"] = "10.0.0.100"

        future = client.SendPacket(req)

    #    loop.run_until_complete(future)
        loop.run_until_complete(asyncio.ensure_future(
            asyncio.gather(
                future,
                return_exceptions=True
            )

        ))

        if future.exception():
            print('EXCEPTION ', future.exception())
        else:
            reply = future.result()

            if reply.code == AccessAccept:
                print("Access accepted")
            else:
                print("Access denied")

            print("Attributes returned by server:")
            for i in reply.keys():
                print("%s: %s" % (i, reply[i]))

        # Close transports
        loop.run_until_complete(asyncio.ensure_future(
            client.deinitialize_transports()))
        print('END')

        del client
    except Exception as exc:
        print('Error: ', exc)
        print('\n'.join(traceback.format_exc().splitlines()))
        # Close transports
        loop.run_until_complete(asyncio.ensure_future(
            client.deinitialize_transports()))

    loop.close()

def test_multi_auth():

    global client

    try:
        # Initialize transports
        loop.run_until_complete(
            asyncio.ensure_future(
                client.initialize_transports(enable_auth=True,
                                             local_addr='127.0.0.1',
                                             local_auth_port=8000,
                                             enable_acct=True,
                                             enable_coa=True)))



        reqs = []
        for i in range(255):
            req = create_request(client, "user%s" % i)
            future = client.SendPacket(req)
            reqs.append(future)

        #    loop.run_until_complete(future)
        loop.run_until_complete(asyncio.ensure_future(
            asyncio.gather(
                *reqs,
                return_exceptions=True
            )

        ))

        for future in reqs:
            if future.exception():
                print('EXCEPTION ', future.exception())
            else:
                reply = future.result()
                print_reply(reply)

        # Close transports
        loop.run_until_complete(asyncio.ensure_future(
            client.deinitialize_transports()))
        print('END')

        del client
    except Exception as exc:
        print('Error: ', exc)
        print('\n'.join(traceback.format_exc().splitlines()))
        # Close transports
        loop.run_until_complete(asyncio.ensure_future(
            client.deinitialize_transports()))

    loop.close()

#test_multi_auth()
test_auth1()