예제 #1
0
파일: loot.py 프로젝트: duongbaoduy/Forban
    def setlastseen(self):

        localfile = os.path.join(self.lootpath, self.luuid, "last")
        tlocalfile = tmpname.get(localfile)
        f = open(tlocalfile[1], "w")
        t = datetime.datetime.now()
        f.write(str(time.mktime(t.timetuple())))
        f.close()
        if os.path.exists(tlocalfile[1]):
            tools.rename(tlocalfile[1], tlocalfile[0])
예제 #2
0
파일: loot.py 프로젝트: duongbaoduy/Forban
    def sethmac(self, lhmac=None):

        localfile = os.path.join(self.lootpath, self.luuid, "hmac")
        tlocalfile = tmpname.get(localfile)

        f = open(tlocalfile[1], "w")
        f.write(lhmac)
        f.close()

        tools.rename(tlocalfile[1], tlocalfile[0])
예제 #3
0
파일: index.py 프로젝트: Joneseh92/Forban
    def save (self, value=None, filepath=None):
        if value is None:
            return False
        if filepath is None:
            return False

        tlocalfile = tmpname.get(filepath, suff=str(os.getpid()))

        f = open(tlocalfile[1], "w")
        f.write(value)
        f.close()

        tools.rename(tlocalfile[1], tlocalfile[0])
예제 #4
0
파일: index.py 프로젝트: Joneseh92/Forban
    def build (self):
        self.index = ""
        for root, dirs, files in os.walk(self.sharedir, topdown=True, followlinks=True):
            for name in files:
                try:
                    self.index = self.index + os.path.join(root.split(self.sharedir)[1],name)+","+str(os.path.getsize(os.path.join(root,name)))+"\n"
                except:
                    pass

        pid = os.getpid()
        workingloc = self.location+"."+str(pid)
        f = open (workingloc,"w")
        f.write(self.index)
        f.close()
        hmacval = self.calchmac(filepath=workingloc)
        hmacpath = self.location+".hmac"
        self.save(value=hmacval, filepath=hmacpath)
        tools.rename(workingloc, self.location)
예제 #5
0
def getFile( srcUrl, dstPath):
    try:
        if not urlExists( srcUrl):
            i = 1
            while ( os.path.isfile( dstPath) ):
                dstPath = rename( dstPath, i)
                i += 1
            urllib.urlretrieve( srcUrl, dstPath)
            addLog( "Logs/logOK", "\"%s\" downloaded" % srcUrl)
            addLog( "Logs/imageOrigins", "%s %s" % ( fileNameWithoutPath( dstPath), srcUrl))
    except Exception:
        reportError( "problem while downloading \"%s\"" % srcUrl)
예제 #6
0
파일: loot.py 프로젝트: duongbaoduy/Forban
    def setname(self, lname):

        localfile = os.path.join(self.lootpath, self.luuid, "name")
        tlocalfile = tmpname.get(localfile)

        f = open(tlocalfile[1], "w")
        f.write(lname)
        f.close()

        tools.rename(tlocalfile[1], tlocalfile[0])

        # self is added for the same forban doing the announce
        # and the discovery

        myid = fid.manage(dynpath=self.dynpath)
        if myid.get() == self.luuid:
            localfile = os.path.join(self.lootpath, self.luuid, "self")
            tlocalfile = tmpname.get(localfile)
            f = open(tlocalfile[1], "w")
            f.write("")
            f.close()
            tools.rename(tlocalfile[1], tlocalfile[0])
예제 #7
0
파일: fetch.py 프로젝트: break123/Forban
        os.makedirs(lpath)

    # as url fetch is part of the Forban protocol interface
    # the Content-Disposition MUST be present even if it's
    # not used right now. The interface is used as file transfert
    # so the Content-Disposition is a requirement for any other
    # HTTP clients

    tlocalfile = tmpname.get(localfile)

    if r.info().has_key('Content-Disposition'):
        f = open (tlocalfile[1], "w")
        try:
            shutil.copyfileobj(r.fp,f)
        except:
            return False
        f.close()
        if os.path.exists(tlocalfile[1]):
            tools.rename(tlocalfile[1], tlocalfile[0])
        return True
    else:
        return False

def managetest():
    #urlget("http://192.168.154.199:12555/s/?g=forban/index")
    print urlget("http://192.168.1.4:12555/s/?g=forban/index")

if __name__ == "__main__":
    managetest()

예제 #8
0
    def on_msg(self, b_data):
        try:
            protocol = self.decode_protocol(b_data)
        except Exception:
            raise Exception(f"数据解析出错了 {b_data}")

        code = protocol["code"]
        size = protocol.get("size")
        msg = protocol["msg"]
        if code == 0:
            print("收到客户端的Ping....")
            return

        elif code == 100:  # 收到了文件流
            if not self.file_fp:  # 首次接收到文件,需要穿件文件写入类
                try:
                    self.file_fp = self.file_io(
                        write_path=protocol["write_path"])
                except Exception as e:
                    print("初始化文件对象错误:", str(e))
                    # self.send_data(200, str(e), dict(allow_send=False))
                    return

            receive_size = 0
            once_recv = self.once_recv
            last_d = b""
            if size > once_recv:  # 文件大于一个接收包
                try:
                    while not size == receive_size:  # 根据协议内说明的文件大小,循环接收,直到接收完毕
                        receive_data = self.base_socket.conn.recv(once_recv)
                        last_d = receive_data
                        if receive_data:
                            receive_size += len(receive_data)
                            self.file_fp.stream_queue.put(
                                receive_data)  # 为了不让IO耽误时间,这里使用工厂模式处理文件的写入
                            if (size -
                                    receive_size) < once_recv:  # 剩余的数据小于一组的大小了
                                once_recv = size - receive_size  # 接收剩余大小
                        else:
                            print("在接受文件流的过程中发生错误,导致接收到空字节,停止接收")
                            self.file_fp.stream_queue.put(protocol)
                            self.file_fp = None
                            return False
                except Exception as e:
                    traceback.print_exc()
                    print("在接收字节流中发生错误", e)
                    time.sleep(3)

            else:
                receive_data = self.base_socket.recv_once(size)
                last_d = receive_data
                self.file_fp.stream_queue.put(
                    receive_data)  # 为了不让IO耽误时间,这里使用工厂模式处理文件的写入
            self.file_fp.stream_queue.put(protocol)
            self.file_fp = None
            print(f"文件写入缓存成功 {os.path.basename(protocol['write_path'])}")

        elif code == 101:  # 客户端需要下载文件
            receive_data = self.base_socket.recv_agroup(size)
            data = tools.decode_dict(receive_data)
            file_list = data["file_list"]
            write_to = data["write_path"]
            print("客户下载文件", file_list)
            self.sendfile_or_mkdir(file_list, write_to)

        elif code == 200:  # 创建新目录
            # 只有服务器才会使用此方法
            receive_data = self.base_socket.recv_agroup(size)
            data = tools.decode_dict(receive_data)
            dir_path = data["dir_path"]
            succ, msg = tools.mkdir(dir_path)
            if succ:
                return self.send_data(201, '')
            return self.error(msg)

        elif code == 202:  # 更改名字
            # 只有服务器才会使用此方法
            receive_data = self.base_socket.recv_agroup(size)
            data = tools.decode_dict(receive_data)
            succ, msg = tools.rename(data["old"], data["new"])
            if succ:
                return self.send_data(203, '')
            return self.error(msg)

        elif code == 204:  # 删除服务器目录或者文件
            # 只有服务器才会使用此方法
            receive_data = self.base_socket.recv_agroup(size)
            data = tools.decode_dict(receive_data)
            succ, msg = tools.remove(data["abs_path"])
            if succ:
                return self.send_data(205, '')
            return self.error(msg)

        elif code == 206:  # 客户端获取服务器目录
            receive_data = self.base_socket.recv_agroup(size)
            data = tools.decode_dict(receive_data)
            list_dir = tools.listdir(data["dir_path"])
            self.send_data(207, data=dict(list_dir=list_dir))

        elif code == 207:  # 服务器返回目录
            receive_data = self.base_socket.recv_agroup(size)
            data = tools.decode_dict(receive_data)
            print("收到服务器目录内容", data)

        elif code == 208:  # 客户端获取服务器磁盘
            disk_list = tools.get_disk()
            self.send_data(207, data=dict(disk_list=disk_list))

        elif code == 209:
            receive_data = self.base_socket.recv_agroup(size)
            data = tools.decode_dict(receive_data)
            disk_list = data["disk_list"]
            print("收到服务器磁盘列表", disk_list)

        elif code == 500:  # 收到异常
            receive_data = self.base_socket.recv_agroup(size)
            data = tools.decode_dict(receive_data)
            print(f"收到异常: {data}")

        elif code == 501:  # 收到通知
            print(f"收到消息: {protocol['msg']}")
예제 #9
0
def clean_boost(platform):
    tools.rename('{0}\\{1}\\include\\{2}\\boost'.format(setup.INSTALL, platform, setup.BOOST_INCLUDE),
                 '{0}\\{1}\\include\\boost'.format(setup.INSTALL, platform))
예제 #10
0
        os.makedirs(lpath)

    # as url fetch is part of the Forban protocol interface
    # the Content-Disposition MUST be present even if it's
    # not used right now. The interface is used as file transfert
    # so the Content-Disposition is a requirement for any other
    # HTTP clients

    tlocalfile = tmpname.get(localfile)

    if r.info().has_key('Content-Disposition'):
        f = open(tlocalfile[1], "w")
        try:
            shutil.copyfileobj(r.fp, f)
        except:
            return False
        f.close()
        if os.path.exists(tlocalfile[1]):
            tools.rename(tlocalfile[1], tlocalfile[0])
        return True
    else:
        return False


def managetest():
    print urlget("http://127.0.0.1:12555/s/?g=forban/index")


if __name__ == "__main__":
    managetest()
예제 #11
0
    def on_msg(self, b_data):
        try:
            protocol = self.decode_protocol(b_data)
        except Exception as e:
            print("解析协议出错", e, b_data, len(b_data))
            return
        try:
            code = protocol["code"]
            next_size = protocol.get("size")
            msg = protocol["msg"]
        except Exception as e:
            print("解析协议错误", e)
            return

        if code == 100:  # 收到了文件流
            print(f"收到文件流 {protocol['write_path']}")
            all_size = next_size
            last_d = b''  # 接收到上一组的数据
            last_second_recv_bytes = int()  # 上一秒接收到的字节数,用于计算下载进度条
            last_second = int(time.time())  # 用来控制间隔一秒更新一次下载进度的变量
            start_time = int(time.time())  # 下载数据开始的时间
            once_recv = self.once_recv  # 规定了一次最多接收多少数据
            receive_size = 0  # 一共接收了多少数据
            detail_size = tools.bytes_to_speed(all_size)  # 需要下载的数据大小以人性化方式展示

            # 首次接收到文件,需要实例化一个文件类
            if not self.file_fp:
                try:
                    self.file_fp = self.file_io(
                        write_path=protocol["write_path"])
                except Exception as e:
                    print("初始化文件对象错误:", str(e))
                    return
            # 当需要接收的数据大于单次最大接收量时,需要进入while循环,直到接收完毕
            if all_size > once_recv:
                while receive_size != all_size:
                    receive_data = self.base_socket.recv_once(once_recv)
                    last_d = receive_data
                    last_second_recv_bytes += len(receive_data)
                    receive_size += len(receive_data)

                    # 为了不让IO耽误时间,这里使用工厂模式处理文件的写入
                    self.file_fp.stream_queue.put(receive_data)

                    # 当剩余的数据小于一组的大小,需要更改下一次接收字节的数量,否则会打乱数据
                    if (all_size - receive_size) < once_recv:
                        once_recv = all_size - receive_size

                    # 每秒钟更新下载进度条
                    if int(time.time()) != last_second and self.set_status:
                        progress = tools.Dict(
                            operation="传送中",
                            name=os.path.basename(protocol["write_path"]),
                            progress=int(receive_size / all_size * 100),
                            speed=tools.bytes_to_speed(last_second_recv_bytes),
                            detail=tools.bytes_to_speed(receive_size) + "/" +
                            detail_size,
                            elapsed_time=tools.second_to_time(
                                int(time.time()) - start_time),
                            remaining_time=tools.second_to_time(
                                (all_size - receive_size) /
                                last_second_recv_bytes))
                        self.set_status.emit(tools.Dict(progress))
                        last_second = int(time.time())
                        last_second_recv_bytes = int()

            else:
                receive_data = self.base_socket.recv_once(all_size)
                last_d = receive_data
                self.file_fp.stream_queue.put(receive_data)
            # 发送一个字典对象,表示写入结束
            self.file_fp.stream_queue.put(protocol)
            self.file_fp = None
            print(
                f"文件写入缓存成功 {os.path.basename(protocol['write_path'])} \n"
                f"文件一共{all_size} 接收了 {receive_size}  最后一组数据长度 {len(last_d)} \n {last_d}"
            )

        elif code == 101:  # 客户端需要下载文件
            receive_data = self.base_socket.recv_agroup(next_size)
            data = tools.decode_dict(receive_data)
            file_list = data["file_list"]
            write_to = data["write_path"]
            print("客户下载文件", file_list)
            self.send_files(file_list, write_to)

        elif code == 200:  # 创建新目录
            # 只有服务器才会使用此方法
            receive_data = self.base_socket.recv_agroup(next_size)
            data = tools.decode_dict(receive_data)
            # if data.get("dir_path") == "C:/test/Keil/C51/Examples/ST uPSD/upsd3300/DK3300-ELCD/I2C/I2C_Master":
            #     print("11111")
            dir_path = data["dir_path"]
            succ, msg = tools.mkdir(dir_path)
            if not succ:
                # return self.send_data(201, '创建新目录成功')
                return self.error(msg)

        elif code == 202:  # 更改名字
            # 只有服务器才会使用此方法
            receive_data = self.base_socket.recv_agroup(next_size)
            data = tools.decode_dict(receive_data)
            succ, msg = tools.rename(data["old"], data["new"])
            if succ:
                return self.send_data(203, '重命名成功')
            return self.error(msg)

        elif code == 204:  #删除服务器目录或者文件
            # 只有服务器才会使用此方法
            receive_data = self.base_socket.recv_agroup(next_size)
            data = tools.decode_dict(receive_data)
            succ, msg = tools.remove(data["abs_path"])
            if succ:
                return self.send_data(205, '删除成功')
            return self.error(msg)

        elif code == 206:  # 客户端获取服务器目录
            receive_data = self.base_socket.recv_agroup(next_size)
            data = tools.decode_dict(receive_data)
            list_dir = tools.listdir(data["dir_path"])
            self.send_data(207, data=dict(list_dir=list_dir))

        elif code == 207:  # 服务器返回目录
            receive_data = self.base_socket.recv_agroup(next_size)
            data = tools.decode_dict(receive_data)
            self.response_data.put(data)

        elif code == 208:  # 客户端获取服务器磁盘
            disk_list = tools.get_disk()
            self.send_data(207, data=dict(disk_list=disk_list))

        elif code == 209:
            receive_data = self.base_socket.recv_agroup(next_size)
            data = tools.decode_dict(receive_data)
            self.response_data.put(data["disk_list"])
            disk_list = data["disk_list"]
            print("收到服务器磁盘列表", disk_list)

        elif code == 210:
            if self.local_reload:
                print("刷新本地文件")
                self.local_reload()

        elif code == 500:  # 收到异常
            receive_data = self.base_socket.recv_agroup(next_size)
            data = tools.decode_dict(receive_data)
            if self.on_error:
                self.on_error(data)
            print(f"收到异常: {data}")

        elif code == 501:  # 收到通知
            print(f"收到消息: {protocol['msg']}")

        elif code == 600:
            if self.show_progress:
                self.show_progress.emit()

        elif code == 601:
            if self.hide_progress:
                self.hide_progress.emit()

        elif code == 602:
            receive_data = self.base_socket.recv_agroup(next_size)
            data = tools.decode_dict(receive_data)
            if self.set_status:
                self.set_status.emit(tools.Dict(data))
예제 #12
0
import reforcer
import signer
import channel
import tools
import os
import sys

path = sys.argv[0]
if path != "main.py":
    index = path.index("main.py")
    dir = path[0:index]
    os.chdir(dir)
# 加固
done = reforcer.reforce()
if done:
    # 签名
    signer.autoSign()
    # 签名后会洗掉渠道信息,需要重新写入
    channel.writeChannel()
    # 重命名
    tools.rename()
else:
    print("============加固失败============")