def main(): """ Front end for fproxyproxy utility """ # default job options verbosity = node.ERROR verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort fproxyHost = os.environ.get("FPROXY_HOST", "127.0.0.1") fproxyPort = int(os.environ.get("FPROXY_PORT", 8888)) listenHost = os.environ.get("FPROXYPROXY_HOST", "127.0.0.1") listenPort = int(os.environ.get("FPROXYPROXY_PORT", 8889)) opts = { "Verbosity": 0, } # process command line switches try: cmdopts, args = getopt.getopt(sys.argv[1:], "?hvH:P:Vp:L:", [ "help", "verbose", "fcpHost=", "fcpPort=", "version", "listenAddress=", "fproxyAddress=", ]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts for o, a in cmdopts: if o in ("-?", "-h", "--help"): help() if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) if o in ("-v", "--verbosity"): verbosity = node.DETAIL opts['Verbosity'] = 1023 verbose = True if o in ("-H", "--fcpHost"): fcpHost = a if o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) if o in ("-L", "--listenAddress"): parts = a.split(":") if len(parts) == 1: listenHost = parts[0] elif len(parts) == 2: if parts[0]: listenHost = parts[0] if parts[1]: listenPort = int(parts[1]) else: usage("Invalid listen address '%s'" % a) if o in ("-p", "--fproxyAddress"): parts = a.split(":") if len(parts) == 1: fproxyHost = parts[0] elif len(parts) == 2: if parts[0]: fproxyHost = parts[0] if parts[1]: fproxyPort = int(parts[1]) # try to create an FCP node, needed for name lookups try: n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, logfile=sys.stderr) log = n._log except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) try: proxy = FProxyProxy(node=n, fproxyHost=fproxyHost, fproxyPort=fproxyPort, listenHost=listenHost, listenPort=listenPort) proxy.run() sys.exit(0) except KeyboardInterrupt: print "fproxyproxy terminated by user" n.shutdown() sys.exit(0) except: traceback.print_exc() print "fproxyproxy terminated" n.shutdown() sys.exit(1)
def main(): """ Front end for fcpput utility """ # default job options verbosity = node.ERROR verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort mimetype = None wait = False opts = { "Verbosity": 0, "persistence": "forever", "async": True, "priority": 3, "Global": "true", "MaxRetries": -1, } # process command line switches try: cmdopts, args = getopt.getopt(sys.argv[1:], "?hvH:P:m:gcdp:wr:et:V", [ "help", "verbose", "fcpHost=", "fcpPort=", "mimetype=", "global", "compress", "disk", "persistence=", "wait", "priority=", "realtime", "timeout=", "version", ]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts makeDDARequest = False opts['nocompress'] = True for o, a in cmdopts: if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) elif o in ("-?", "-h", "--help"): help() sys.exit(0) elif o in ("-v", "--verbosity"): if verbosity < node.DETAIL: verbosity = node.DETAIL else: verbosity += 1 opts['Verbosity'] = 1023 verbose = True elif o in ("-H", "--fcpHost"): fcpHost = a elif o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) elif o in ("-m", "--mimetype"): mimetype = a elif o in ("-c", "--compress"): opts['nocompress'] = False elif o in ("-d", "--disk"): makeDDARequest = True elif o in ("-p", "--persistence"): if a not in ("connection", "reboot", "forever"): usage( "Persistence must be one of 'connection', 'reboot', 'forever'" ) opts['persistence'] = a elif o in ("-g", "--global"): opts['Global'] = "true" elif o in ("-w", "--wait"): opts['async'] = False wait = True elif o in ("-r", "--priority"): try: pri = int(a) if pri < 0 or pri > 6: raise hell except: usage("Invalid priority '%s'" % pri) opts['priority'] = int(a) elif o in ("-e", "--realtime"): opts['realtime'] = True elif o in ("-t", "--timeout"): try: timeout = node.parseTime(a) except: usage("Invalid timeout '%s'" % a) opts['timeout'] = timeout # process args nargs = len(args) if nargs < 1 or nargs > 2: usage("Invalid number of arguments") keytypes = ["USK", "KSK", "SSK", "CHK"] if nargs == 2: infile = args[1] uri = args[0] if not uri.startswith("freenet:"): uri = "freenet:" + uri if not uri[len("freenet:"):len("freenet:") + 3] in keytypes: print uri, uri[len("freenet:"):len("freenet:") + 4] usage("The first argument must be a key. Example: CHK@/<filename>") else: # if no infile is given, automatically upload to a CHK key. infile = args[0] uri = "freenet:CHK@/" + node.toUrlsafe(infile) # if we got an infile, but the key does not have the filename, use that filename for the uri. if infile and uri[-2:] == "@/" and uri[:3] in keytypes: uri += node.toUrlsafe(infile) # figure out a mimetype if none present if infile and not mimetype: base, ext = os.path.splitext(infile) if ext: mimetype = mimetypes.guess_type(ext)[0] if mimetype: # mimetype explicitly specified, or implied with input file, # stick it in. # otherwise, let FCPNode.put try to imply it from a uri's # 'file extension' suffix opts['mimetype'] = mimetype # try to create the node try: n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, logfile=sys.stderr) except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) # FIXME: Throw out all the TestDDARequest stuff. It is not needed for putting a single file. TestDDARequest = False #: The key of the uploaded file. freenet_uri = None if makeDDARequest: if infile is not None: ddareq = dict() ddafile = os.path.abspath(infile) ddareq["Directory"] = os.path.dirname(ddafile) # FIXME: This does not work. The only reason why testDDA # works is because there is an alternate way of specifying # a content hash, and that way works. ddareq["WantReadDirectory"] = "True" ddareq["WantWriteDirectory"] = "false" print "Absolute filepath used for node direct disk access :", ddareq[ "Directory"] print "File to insert :", os.path.basename(ddafile) TestDDARequest = n.testDDA(**ddareq) if TestDDARequest: opts["file"] = ddafile putres = n.put(uri, **opts) else: sys.stderr.write( "%s: disk access failed to insert file %s fallback to direct\n" % (progname, ddafile)) else: sys.stderr.write("%s: disk access needs a disk filename\n" % progname) # try to insert the key using "direct" way if dda has failed if not TestDDARequest: # grab the data if not infile: data = sys.stdin.read() else: try: data = file(infile, "rb").read() except: n.shutdown() usage("Failed to read input from file %s" % repr(infile)) try: #print "opts=%s" % str(opts) # give it the file anyway: Put is more intelligent than this script. opts["data"] = data if infile: opts["file"] = infile n.listenGlobal() putres = n.put(uri, **opts) if not wait: opts["chkonly"] = True opts["async"] = False # force the node to be fast opts["priority"] = 0 opts["realtime"] = True opts["persistence"] = "connection" opts["Global"] = False freenet_uri = n.put(uri, **opts) except: if verbose: traceback.print_exc(file=sys.stderr) n.shutdown() sys.stderr.write("%s: Failed to insert key %s\n" % (progname, repr(uri))) sys.exit(1) if not wait: # got back a job ticket, wait till it has been sent putres.waitTillReqSent() else: # successful, return the uri sys.stdout.write(putres) sys.stdout.flush() n.shutdown() # output the key of the file if not wait: print freenet_uri # all done sys.exit(0)
def main(): """ Front end for fcpget utility """ # default job options verbosity = node.ERROR verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort opts = { "Verbosity": 0, } # process command line switches try: cmdopts, args = getopt.getopt(sys.argv[1:], "?hvH:P:V", [ "help", "verbose", "fcpHost=", "fcpPort=", "version", ]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts for o, a in cmdopts: if o in ("-?", "-h", "--help"): help() if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) if o in ("-v", "--verbosity"): verbosity = node.DETAIL opts['Verbosity'] = 1023 verbose = True if o in ("-H", "--fcpHost"): fcpHost = a if o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) # try to create the node try: n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, logfile=sys.stderr) except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) # determine the uris if len(args) != 2: usage("Invalid number of arguments") uriSrc = args[0].strip() uriDest = args[1].strip() # do the invert uriPub = n.redirect(uriSrc, uriDest) n.shutdown() # successful, return the uri sys.stdout.write(uriPub) sys.stdout.flush() # all done sys.exit(0)
def main(): """ Front end for fcpput utility """ # default job options verbosity = node.ERROR verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort mimetype = None nowait = False opts = { "Verbosity" : 0, "persistence" : "connection", "async" : False, "priority" : 3, "MaxRetries" : -1, } # process command line switches try: cmdopts, args = getopt.getopt( sys.argv[1:], "?hvH:P:m:gcdp:nr:t:V", ["help", "verbose", "fcpHost=", "fcpPort=", "mimetype=", "global","compress","disk", "persistence=", "nowait", "priority=", "timeout=", "version", ] ) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts makeDDARequest=False opts['nocompress'] = True for o, a in cmdopts: if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) elif o in ("-?", "-h", "--help"): help() sys.exit(0) elif o in ("-v", "--verbosity"): if verbosity < node.DETAIL: verbosity = node.DETAIL else: verbosity += 1 opts['Verbosity'] = 1023 verbose = True elif o in ("-H", "--fcpHost"): fcpHost = a elif o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) elif o in ("-m", "--mimetype"): mimetype = a elif o in ("-c", "--compress"): opts['nocompress'] = False elif o in ("-d","--disk"): makeDDARequest=True elif o in ("-p", "--persistence"): if a not in ("connection", "reboot", "forever"): usage("Persistence must be one of 'connection', 'reboot', 'forever'") opts['persistence'] = a elif o in ("-g", "--global"): opts['Global'] = "true" elif o in ("-n", "--nowait"): opts['async'] = True nowait = True elif o in ("-r", "--priority"): try: pri = int(a) if pri < 0 or pri > 6: raise hell except: usage("Invalid priority '%s'" % pri) opts['priority'] = int(a) elif o in ("-t", "--timeout"): try: timeout = node.parseTime(a) except: usage("Invalid timeout '%s'" % a) opts['timeout'] = timeout # process args nargs = len(args) if nargs < 1 or nargs > 2: usage("Invalid number of arguments") uri = args[0] if not uri.startswith("freenet:"): uri = "freenet:" + uri # determine where to get input if nargs == 1 or args[1] == '-': infile = None else: infile = args[1] # figure out a mimetype if none present if infile and not mimetype: filename = os.path.basename(infile) if filename: mimetype = mimetypes.guess_type(filename)[0] if mimetype: # mimetype explicitly specified, or implied with input file, # stick it in. # otherwise, let FCPNode.put try to imply it from a uri's # 'file extension' suffix opts['mimetype'] = mimetype # try to create the node try: n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, logfile=sys.stderr) except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) if makeDDARequest: if infile is not None: ddareq=dict() ddafile = os.path.abspath(infile) ddareq["Directory"]= os.path.dirname(ddafile) ddareq["WantReadDirectory"]="True" ddareq["WantWriteDirectory"]="False" print "Absolute filepath used for node direct disk access :",ddareq["Directory"] print "File to insert :",os.path.basename( ddafile ) TestDDARequest=n.testDDA(**ddareq) print "Result of dda request :",TestDDARequest if TestDDARequest: opts["file"]=ddafile uri = n.put(uri,**opts) else: sys.stderr.write("%s: disk access failed to insert file %s fallback to direct\n" % (progname,ddafile) ) else: sys.stderr.write("%s: disk access need a disk filename\n" % progname ) # try to insert the key using "direct" way if dda has failed if not TestDDARequest: # grab the data if not infile: data = sys.stdin.read() else: try: data = file(infile, "rb").read() except: n.shutdown() usage("Failed to read input from file %s" % repr(infile)) try: #print "opts=%s" % str(opts) uri = n.put(uri, data=data, **opts) except: if verbose: traceback.print_exc(file=sys.stderr) n.shutdown() sys.stderr.write("%s: Failed to insert key %s\n" % (progname, repr(uri))) sys.exit(1) if nowait: # got back a job ticket, wait till it has been sent uri.waitTillReqSent() else: # successful, return the uri sys.stdout.write(uri) sys.stdout.flush() n.shutdown() # all done sys.exit(0)
def main(): """ Front end for fcpget utility """ # default job options verbosity = node.ERROR verbose = False fcpHost = node.defaultFCPHost fcpPort = node.defaultFCPPort mimetype = None cfgfile = None opts = { "Verbosity": 0, } # process command line switches try: cmdopts, args = getopt.getopt(sys.argv[1:], "?hvH:P:Vc:", [ "help", "verbose", "fcpHost=", "fcpPort=", "version", "config-file=", ]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False #print cmdopts for o, a in cmdopts: if o in ("-?", "-h", "--help"): help() if o in ("-V", "--version"): print "This is %s, version %s" % (progname, node.fcpVersion) sys.exit(0) if o in ("-c", "--config-file"): cfgfile = a if o in ("-v", "--verbosity"): verbosity = node.DETAIL opts['Verbosity'] = 1023 verbose = True if o in ("-H", "--fcpHost"): fcpHost = a if o in ("-P", "--fcpPort"): try: fcpPort = int(a) except: usage("Invalid fcpPort argument %s" % repr(a)) # try to create the node try: n = node.FCPNode(host=fcpHost, port=fcpPort, verbosity=verbosity, logfile=sys.stderr, namesitefile=cfgfile) except: if verbose: traceback.print_exc(file=sys.stderr) usage("Failed to connect to FCP service at %s:%s" % (fcpHost, fcpPort)) if len(args) < 1: usage("No command specified") mgr = NamesMgr(n) res = mgr.execute(*args) n.shutdown() if res: sys.exit(1) else: sys.exit(0)