Esempio n. 1
0
 def index(self):
     if not utils.auth(self, cherrypy.session):
         raise cherrypy.HTTPRedirect("/login")
     if self.Game.winner:
         field_template = self.env.get_template('field_disabled.j2')
         WELCOME_TEXT = "And the Winner is: %s" % self.Game.winner.name
     else:
         field_template = self.env.get_template('field.j2')
         WELCOME_TEXT = self.name
     fields_string = self.render_fields(field_template)
     if self.Game.winner:
         fields_string = fields_string + self.render_fields(
             field_template, self.Game.winner)
     tmpl = self.env.get_template('slice.j2')
     url = urlparse(cherrypy.url())
     WEBSOCKET = "%s/%s/subscribe" % (url.netloc, self.Game.name)
     STATS = ""
     for Slice in self.Game.slices.values():
         STATS = STATS + "%s: %s<br>" % (Slice.name, Slice.score)
     STATS = STATS.strip("<br>")
     return tmpl.render(GAME=self.Game.name,
                        WELCOME_TEXT=WELCOME_TEXT,
                        PLAYER=self.name,
                        FIELDS=fields_string,
                        WEBSOCKET=WEBSOCKET,
                        STATS=STATS)
Esempio n. 2
0
 def post_asset(self, server_info):
     data = encrypt(json.dumps(server_info))
     requests.post(
         url=settings.API,
         data=data,
         headers={'OpenKey': auth(),'Content-Type':'application/json'}
     )
Esempio n. 3
0
    def port_asset(self, serverinfo):
        obj = PrpCrypt(settings.DATA_KEY)

        serverinfo = {
            'board': {
                'status': True,
                'data': {
                    'sn': '2102310TQE10DC000344'
                }
            },
            'cpu': {
                'status': True,
                'data': {
                    'cpu_model': ' Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz'
                }
            },
            'disk': {
                'status': True,
                'data': {
                    'total': '123G'
                }
            },
            'memory': {
                'status': True,
                'data': {
                    'total': '81G'
                }
            },
            'nic': {
                'status': True,
                'data': {
                    'enp1s0f0': {
                        'up': True,
                        'hwaddr': '48:46:fb:f8:69:05',
                        'ipaddrs': '',
                        'netmask': ''
                    },
                    'enp1s0f1': {
                        'up': True,
                        'hwaddr': '48:46:fb:f8:69:66',
                        'ipaddrs': '192.168.1.15',
                        'netmask': '255.255.255.0'
                    }
                }
            }
        }

        data = obj.encrypt(json.dumps(serverinfo))
        try:
            requests.post(
                url=settings.API,
                data=data,
                headers={
                    'OpenKey': auth(),
                    'Content-Type': 'application/json'
                },
            )
            print('发送成功')
        except:
            print('发送信息失败')
Esempio n. 4
0
 def post_asset(self, server_info):
     data = encrypt(json.dumps(server_info))
     requests.post(url=settings.API,
                   data=data,
                   headers={
                       'OpenKey': auth(),
                       'Content-Type': 'application/json'
                   })
Esempio n. 5
0
 def post_asset(self, server_info):
     '''向api发送资产信息'''
     data = encrypt(json.dumps(server_info))
     requests.post(
         setting.API,
         data=data,
         headers={
             'ContentType': 'application/json',
             'Openkey': auth()
         }
     )  #json打包发过去,会默认发送一个header={'content-type':'application/json'},所有接受方只能在request.body里面接受到,request.post是没有值的
Esempio n. 6
0
    def post_asset(self, server_info):
        #加密
        data = encrypt(json.dumps(server_info))

        result = requests.post(
            url=settings.API,
            headers={
                "OpenKey": auth(),
                "Content-Type": "application/json"
            },
            data=data,
        )
        return result
Esempio n. 7
0
 def post_asset(self, server_info):
     # 将数据转换成json字符串格式发送
     data = encrypt(
         json.dumps(server_info))  # 将字典格式的数据转换成encrypt所需的字符串格式,然后加密
     response = requests.post(
         url=settings.API,
         data=data,
         headers={
             'OpenKey': auth(),
             'Content-Type': 'application/json'
         }  # 
     )
     print(response.text)
Esempio n. 8
0
    def index(self):
        cherrypy.session.debug = True
        login_tmpl = self.env.get_template('login.j2')
        tmpl = self.env.get_template('index.j2')
        initial_wordlist = ""
        game_list = self.games.keys()
        wordlist_ids = self.wordlists.keys()

        if not utils.auth(self, cherrypy.session):
            LOGIN = login_tmpl.render()
        else:
            LOGIN = "******" % cherrypy.session.get(
                'player').name

        return tmpl.render(GAME_LIST=game_list,
                           WORDLIST_IDS=wordlist_ids,
                           INITIAL_WORDLIST=self.wordlists[wordlist_ids[0]],
                           LOGIN=LOGIN)
Esempio n. 9
0
    def get_post(self):
        import json
        import hashlib
        '''获取未采集的主机列表'''
        import time
        ctime = time.time()
        new_key = "%s|%s" % (setting.OPENKEY, ctime)  # 结合
        m = hashlib.md5()
        m.update(bytes(new_key, encoding='utf-8'))
        md5_key = m.hexdigest()

        md5_time_key = "%s|%s" % (md5_key, ctime)

        response = requests.get(setting.API, headers={'Openkey':
                                                      auth()})  #获取请求的时候要做api验证
        result = json.loads(response.text)
        if not result['status']:
            return
        else:
            return result[
                'data']  #{'status':true,'data':['c1.com,'c2.com']}  主机列表
Esempio n. 10
0
    def submit(self):
        cherrypy.session.load()

        cl = cherrypy.request.headers['Content-Length']
        rawbody = cherrypy.request.body.read(int(cl))
        body = json.loads(rawbody)

        pprint.pprint(body)
        print

        #all actions that can be performed without the user being authenticated

        if body["mode"] == "login":
            # $(sanity_check)
            player = body["player"]
            secret = body["secret"]
            if player in self.players.keys(
            ) and secret == self.players[player].secret:
                cherrypy.session.__setitem__("player", self.players[player])
                return self.craft_response({
                    "mode": "success",
                    "message": "You are now logged in"
                })
            else:
                return self.craft_response({
                    "mode": "failure",
                    "message": "Login failed!"
                })

        elif body["mode"] == "register":
            player = body["player"]
            secret = body["secret"]
            if player in self.players.keys():
                return self.craft_response({
                    "mode": "failure",
                    "message": "Player exists!"
                })
            else:
                new_player = Player(name=player, secret=secret)
                self.players[player] = new_player
                cherrypy.session.__setitem__("player", self.players[player])
                self.save_players()
                return self.craft_response({
                    "mode": "success",
                    "message": "Player created"
                })

        elif body["mode"] == "join_as_spectator":
            game_name = body['game_name']
            print "not implemented"
            return self.craft_response({
                "mode": "failure",
                "message": "not implemented"
            })

        elif body["mode"] == "get_wordlist":
            if body['wordlist'] in self.wordlists.keys():
                return self.craft_response({
                    "mode":
                    "success",
                    "return_data":
                    '\n'.join(self.wordlists[body['wordlist']])
                })

        elif body['mode'] == "get_game_description":
            pprint.pprint(self.games)
            return "foobar"

        #check if the user is authenticated and redirect to /login when he isnt

        if not utils.auth(self, cherrypy.session):
            return self.craft_response({"mode": "redirect", "uri": '/login'})

        #these actions can only be performed when the user is authenticated

        if body["mode"] == "create_game":
            title = body["title"]
            wordlist = body["wordlist"].splitlines()
            game_uri = self.create_game(name=title, wordlist=wordlist)
            if game_uri:
                return self.craft_response({
                    "mode": "success",
                    "message": 'new game created'
                })
            return self.craft_response({
                "mode":
                "failure",
                "message":
                "could not create game, does it exist?"
            })

        elif body["mode"] == "join_game":
            game = body["game"]
            player = cherrypy.session.get('player')
            return self.craft_response({
                "mode":
                "redirect",
                "uri":
                self.games[game].generate_slice(player=player, env=self.env)
            })
            #    return self.craft_response({"mode": "redirect", "uri": self.games[game].slices[player.name].uri})
            #uri = self.games[game].slices[player.name].uri
            #self.craft_response({"mode": "redirect", "uri": uri})

        elif body["mode"] == "check_field":
            game = body["game"]
            word_id = int(body["word_id"])
            player = cherrypy.session.get('player')
            self.games[game].fields[word_id].check_field(player)
            return self.craft_response({"mode": "reload"})

        else:
            return 404

        return 503