aboutsummaryrefslogtreecommitdiff
path: root/fifconfig.py
blob: 3dae32768c0ee8c3dbdb1ece13e29a310207652c (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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python3
# File: fifconfig.py
# Location: https://gitlab.com/bgstack15/fifconfig
# Author: bgstack15
# SPDX-License-Identifier: GPL-3.0
# Startdate: 2022-03-15 20:35
# Title: Flask-ifconfig
# Purpose: Act similar to http://ifconfig.me but with flask, for my network testing usage
# History:
# Usage:
# Reference:
#    https://tedboy.github.io/flask/generated/generated/flask.Request.html
#    https://bgstack15.ddns.net/cgit/stackbin/tree/stackbin.py
#    https://pypi.org/project/dicttoxml/
# Improve:
# Dependencies:
#    pip-devuan: json2html
#    pip-centos7: json2html
#    dep-devuan: python3-dicttoxml | python3-xmltodict
#    dep-centos7: python36-xmltodict
#    reverse-proxy configs that include X-Forwarded-Prefix headers.
# Documentation: see README.md

from flask import Flask, request, jsonify, url_for
import os

app = Flask(__name__)
try:
   app.config.from_pyfile(os.environ['FIFCONFIG_CONF'])
except:
   app.config.from_pyfile('fifconfig.conf')

def _make_dict_safe_for_text(response):
   """ Mostly for converting the silly x-forwarded-for ImmutableList so that it does not show that class name. """
   # This works except for the silly "ImmutableList([" text output for that one 'via' item.
   #response = { i:str(response[i]) for i in response }
   # So instead of that beautiful oneliner, you get this stupid 9-line block.
   gen = (i for i in response)
   new_response = {}
   for i in gen:
      if "<class 'werkzeug.datastructures.ImmutableList'>" == str(type(response[i])):
         #print(f"i {i} is a immutablelist")
         new_response[i] = ','.join(response[i])
      else:
         new_response[i] = str(response[i])
   return new_response

def get_attribs(
   request,
   lia = False,
   ia = False,
   ua = False,
   l = False,
   re = False,
   m = False,
   e = False,
   mt = False,
   c = False,
   xff = False
):
   """
   Main function that builds the desired response dict
   that will later be turned into json or html by the
   various small @app.route functions.
   """
   response = {}
   r = request
   rh = r.headers
   if lia:
      response['lastipaddress'] = r.remote_addr
   if ia:
      response['ipaddress'] = r.remote_addr
      if r.access_route and len(r.access_route) > 0:
         response['ipaddress'] = r.access_route[0]
   if ua:
      response['useragent'] = str(r.user_agent)
   if l:
      response['language'] = r.accept_languages
   if re:
      response['referer'] = r.referrer
   if m:
      response['method'] = r.method
   if e:
      response['encoding'] = r.accept_encodings
   if mt:
      response['mimetype'] = r.accept_mimetypes
   if c:
      response['charset'] = r.accept_charsets
   if xff:
      #response['x-forwarded-for'] = rh.get('X-Forwarded-For') or ''
      response['x-forwarded-for'] = r.access_route
   #'charset': rh.get('Accept-Charset') or '',
   #'endpoint': request.endpoint
   # via is the same as x-forwarded-for
   #'via': r.access_route
   print(f"DEBUG: dict is {response}")
   return response

def prepare_output(request, response):
   """
   Used to customize the output to json, html, or text depending on what the client asked for. In order of most important to least important:
      1. request argument, i.e., '?json'
      2. Accept-Mimetypes header
   """
   possible_formats = ['json','html','text','xml']
   _format = "text"
   # priority two
   for i in request.accept_mimetypes:
      for j in i:
         if 'application/xml' == j and _format != "html":
            _format = "xml"
         if 'application/json' == j:
            _format = "json"
         if 'application/xhtml+xml' == j or 'text/html' == j:
            _format = "html"
         if 'text/plain' == j:
            _format = "text"
   # priority one
   if request.args:
      if 'xml' in request.args:
         _format = "xml"
      if 'json' in request.args:
         _format = "json"
      if 'html' in request.args:
         _format = "html"
      if 'text' in request.args:
         _format = "text"
   if _format not in possible_formats:
      print(f"DEBUG (prepare_output): how did format {_format} get defined?! Using text.")
      _format = "text"
   # main process
   if "html" == _format:
      print("Sending html")
      if True:
         from json2html import json2html
         response = _make_dict_safe_for_text(response)
         response = json2html.convert(json = response)
         if not 'nolinks' in request.args:
            prefix = ''
            if 'HTTP_X_FORWARDED_PREFIX' in request.environ:
               prefixes = request.environ['HTTP_X_FORWARDED_PREFIX']
               prefix = ''.join(prefixes.split(',')).replace(' ','')
            response += f"<div style='font-size: 80%;'>"
            response += f"<a href='{prefix}{url_for('ip')}'>ip</a> "
            response += f"<a href='{prefix}{url_for('ua')}'>ua</a> "
            response += f"<a href='{prefix}{url_for('lang')}'>lang</a> "
            response += f"<a href='{prefix}{url_for('encoding')}'>encoding</a> "
            response += f"<a href='{prefix}{url_for('mime')}'>mime</a> "
            response += f"<a href='{prefix}{url_for('charset')}'>charset</a> "
            response += f"<a href='{prefix}{url_for('forwarded')}'>forwarded</a> "
            #response += f"<p>{request.environ}<p>"
            response += f"<a href='{app.config['SOURCE_URL']}'>SOURCE</a>"
            response += f"</div>"
         return response
      else:
         print("Unable to load json2html, sending plain text.")
         return str(response)
   elif "xml" == _format:
      print("Sending xml")
      pretty = False
      if request.args and 'pretty' in request.args:
         pretty = True
      try:
         import xmltodict
         response = xmltodict.unparse({'info':response})
      except:
         try:
            # This lib is objectively better but not available natively on CentOS 7
            print("Trying dicttoxml")
            from dicttoxml import dicttoxml
            response = dicttoxml(response,custom_root="info")
         except:
            print("Unable to load xmltodict, sending plain text.")
            return str(response)
      if pretty:
         try:
            from xml.dom.minidom import parseString
            response = parseString(response).toprettyxml()
         except:
            pass
      return response
   elif "json" == _format:
      print("Sending json")
      return jsonify(response)
   else:
      # the only other option is text
      print("Sending text")
      response = _make_dict_safe_for_text(response)
      new_response = ""
      if 1 == len(response):
         for i in response:
            return response[i]
      else:
         for i in response:
            new_response = new_response + '\n' + str(i) + ': ' + str(response[i])
      return new_response

@app.route('/')
def root():
   response = get_attribs(request, lia = False, ia = True, ua = True, l = True, re = True, m = True, e = True, mt = True, c = True, xff = True)
   response = prepare_output(request, response)
   return response

@app.route('/ip')
def ip():
   return prepare_output(request, get_attribs(request, ia = True))

@app.route('/ua')
def ua():
   return prepare_output(request, get_attribs(request, ua = True))

@app.route('/lang')
def lang():
   return prepare_output(request, get_attribs(request, l = True))

@app.route('/encoding')
def encoding():
   return prepare_output(request, get_attribs(request, e = True))

@app.route('/mime')
def mime():
   return prepare_output(request, get_attribs(request, mt = True))

@app.route('/charset')
def charset():
   return prepare_output(request, get_attribs(request, c = True))

@app.route('/forwarded')
def forwarded():
   return prepare_output(request, get_attribs(request, xff = True))

if __name__ == "__main__":
   app.run()
bgstack15