def do_rename(self, new_name): file_set, proto_path = next( load_proto_msgs(self.app.current_req_proto, True), None) file_set = file_set.file """ First, recursively iterate over descriptor fields until we have a numeric path that leads to it in the structure, as explained in [1] above """ for file_ in file_set: field_path = self.find_path_for_field(file_.message_type, [4], file_.package) if field_path: for location in file_.source_code_info.location: if location.path == field_path: start_line, start_col, end_col = location.span[:3] # Once we have file position information, do # write the new field name in .proto file_path = str(proto_path / file_.name) with open(file_path) as fd: lines = fd.readlines() assert lines[start_line][ start_col:end_col] == self.text(0).strip('+ ') lines[start_line] = lines[start_line][:start_col] + new_name + \ lines[start_line][end_col:] with open(file_path, 'w') as fd: fd.writelines(lines) # Update the name on GUI items corresponding to # this field and its duplicates (if repeated) obj = self while obj.orig_obj: obj = obj.orig_obj while obj: obj.full_name = obj.full_name.rsplit( '.', 1)[0] + '.' + new_name self.app.ds_full_names[id(obj.ds)] = obj.full_name obj.setText(0, new_name + '+' * self.repeated + ' ') obj = obj.dupe_obj return True
def new_endpoint(self, path): if not self.proto_fs.isDir(path): path = self.proto_fs.filePath(path) if assert_installed(self.choose_proto, binaries=['protoc']): if not getattr(self, 'only_resp_combo', False): self.create_endpoint.pbRequestCombo.clear() self.create_endpoint.pbRespCombo.clear() has_msgs = False for name, cls in load_proto_msgs(path): has_msgs = True if not getattr(self, 'only_resp_combo', False): self.create_endpoint.pbRequestCombo.addItem( name, (path, name)) self.create_endpoint.pbRespCombo.addItem( name, (path, name)) if not has_msgs: QMessageBox.warning( self.view, ' ', 'There is no message defined in this .proto.') return self.create_endpoint.reqDataSubform.hide() if not getattr(self, 'only_resp_combo', False): self.create_endpoint.endpointUrl.clear() self.create_endpoint.transports.clear() self.create_endpoint.sampleData.clear() self.create_endpoint.pbParamKey.clear() self.create_endpoint.parsePbCheckbox.setChecked(False) for name, meta in transports.items(): item = QListWidgetItem(meta['desc'], self.create_endpoint.transports) item.setData(Qt.UserRole, (name, meta.get('ui_data_form'))) elif getattr(self, 'saved_transport_choice'): self.create_endpoint.transports.setCurrentItem( self.saved_transport_choice) self.pick_transport(self.saved_transport_choice) self.saved_transport_choice = None self.only_resp_combo = False self.set_view(self.create_endpoint)
def launch_fuzzer(self, item): if type(item) == int: data, sample_id = self.fuzzer.comboBox.itemData(item) else: data, sample_id = item.data(Qt.UserRole), 0 if data and assert_installed(self.view, binaries=['protoc']): self.current_req_proto = BASE_PATH / 'protos' / data['request'][ 'proto_path'] self.pb_request = load_proto_msgs(self.current_req_proto) self.pb_request = dict( self.pb_request)[data['request']['proto_msg']]() if data.get('response') and data['response']['format'] == 'raw_pb': self.pb_resp = load_proto_msgs(BASE_PATH / 'protos' / data['response']['proto_path']) self.pb_resp = dict( self.pb_resp)[data['response']['proto_msg']] self.pb_param = data['request'].get('pb_param') self.base_url = data['request']['url'] self.endpoint = data self.transport_meta = transports[data['request']['transport']] self.transport = self.transport_meta['func'](self.pb_param, self.base_url) sample = '' if data['request'].get('samples'): sample = data['request']['samples'][sample_id] self.get_params = self.transport.load_sample( sample, self.pb_request) # Get initial data into the Protobuf tree view self.fuzzer.pbTree.clear() self.parse_desc(self.pb_request.DESCRIPTOR, self.fuzzer.pbTree) self.parse_fields(self.pb_request) # Do the same for transport-specific data self.fuzzer.getTree.clear() if self.transport_meta.get('ui_tab'): self.fuzzer.tabs.setTabText(1, self.transport_meta['ui_tab']) if self.get_params: for key, val in self.get_params.items(): ProtocolDataItem(self.fuzzer.getTree, key, val, self) else: self.fuzzer.tabs.setTabText(1, '(disabled)') # how to hide it ? # Fill the request samples combo box if we're loading a new # endpoint. if type(item) != int: if len(data['request'].get('samples', [])) > 1: self.fuzzer.comboBox.clear() for sample_id, sample in enumerate( data['request']['samples']): self.fuzzer.comboBox.addItem( sample[self.pb_param] if self.pb_param else str(sample), (data, sample_id)) self.fuzzer.comboBoxLabel.show() self.fuzzer.comboBox.show() else: self.fuzzer.comboBoxLabel.hide() self.fuzzer.comboBox.hide() self.set_view(self.fuzzer) self.fuzzer.frame.setUrl(QUrl("about:blank")) self.update_fuzzer()